Beispiel #1
0
void *fs_load(const char *path, int *datalen)
{
    fs_file fh;
    void *data;

    data = NULL;

    if ((*datalen = fs_size(path)) > 0)
        if ((fh = fs_open_read(path)))
        {
            if ((data = malloc(*datalen)))
            {
                if (fs_read(data, *datalen, 1, fh) != 1)
                {
                    free(data);
                    data = NULL;
                    *datalen = 0;
                }
            }

            fs_close(fh);
        }

    return data;
}
Beispiel #2
0
FILE *fdopen(uint64_t fd, const char *mode) {
	FILE *stream;
	
	if (!fd) {
		return NULL;
	}

	stream = calloc(sizeof(FILE), 1);

	if (!stream) {
		errno = ENOMEM;
		return NULL;
	}

	stream->fd       = fd;
	stream->mutex    = false;
	stream->position = 0;
	stream->size     = fs_size(fd);
	stream->buffer   = NULL;
	stream->buffsize = 0;
	stream->buffpos  = 0;
	stream->revbuf   = EOF;
	stream->flags    = FILE_NBF | FILE_READ | FILE_WRITE;

	return stream;
}
Beispiel #3
0
int16_t
parse_cmd_fs_list (char *cmd, char *output, uint16_t len)
{
  char name[FS_FILENAME + 1];

  if (cmd[0] != 0x05) 
    {
      /* first function entry */
      cmd[0] = 0x05;		/* set magic byte ... */
      cmd[1] = 0x00;

      return ECMD_AGAIN(snprintf_P(output, len, PSTR("name  :inode :size\n"
						     "----------------------")));
    }
  else
    {
      if (fs_list (&fs, NULL, name, cmd[1] ++) != FS_OK)
	return ECMD_FINAL_OK;		/* no repare, out. */
      
      name[FS_FILENAME] = 0;

      fs_inode_t inode = fs_get_inode (&fs, name);
      fs_size_t size = fs_size (&fs, inode);

      return ECMD_AGAIN(snprintf_P(output, len, PSTR("%-6s:0x%04x:0x%04x"),
			            name, inode, size));
    }
}
Beispiel #4
0
block_off_t parity_allocated_size(struct snapraid_state* state)
{
	block_off_t parity_block;
	tommy_node* i;

	/* compute the size of the parity file */
	parity_block = 0;
	for (i = state->disklist; i != 0; i = i->next) {
		struct snapraid_disk* disk = i->data;

		/* start from the declared size */
		block_off_t block = fs_size(disk);

		/* decrease the block until an allocated one, but part of a file */
		/* we don't stop at deleted blocks, because we want to have them cleared */
		/* if they are at the end of the parity */
		while (block > parity_block && !block_has_file(fs_par2block_find(disk, block - 1)))
			--block;

		/* get the highest value */
		if (block > parity_block)
			parity_block = block;
	}

	return parity_block;
}
Beispiel #5
0
/**
 * Periodically send the next chunk of the file
 * @param app pointer to a Files application
 */
static void send_task(void * param)
{
    lv_app_inst_t * app = param;
    my_app_data_t * app_data = app->app_data;

    if(app_data->send_in_prog == 0) return;

    /*Read a chunk*/
    uint32_t rn;
    char rd_buf[LV_APP_FILES_CHUNK_MAX_SIZE];
    fs_res_t res = fs_read(&app_data->file, rd_buf, app_data->chunk_size, &rn);
    if(res == FS_RES_OK) {
       app_data->send_in_prog = 1;
       lv_app_com_send(app, LV_APP_COM_TYPE_CHAR, rd_buf, rn);
    }

    /*If the read failed close the file and show an error*/
    if(res != FS_RES_OK) {
       fs_close(&app_data->file);
       app_data->send_in_prog = 0;
       lv_app_notice_add("Can not send\nthe file in Files");
    }
    /*If the read was successful*/
    else {
        my_sc_data_t * sc_data = app->sc_data;

        /*If the file is read close it a show a notification*/
        if(rn < app_data->chunk_size) {
            lv_app_notice_add("File sent");
            fs_close(&app_data->file);
            app_data->send_in_prog = 0;

            /*Refresh the shortut*/
            if(sc_data != NULL) {
                lv_label_set_text(sc_data->label, fs_get_last(app_data->path));
                lv_obj_align(sc_data->label, NULL, LV_ALIGN_CENTER, 0, 0);
            }
        }
        /*If the file is not sent yet refresh the shortcut with percentage of sending*/
        else {
            if(sc_data != NULL) {
                uint32_t size;
                fs_size(&app_data->file, &size);
                uint32_t pos;
                fs_tell(&app_data->file, &pos);

                uint8_t pct = (uint32_t) (pos * 100) / size;

                char buf[256];
                sprintf(buf, "Sending\n%d%%", pct);
                lv_label_set_text(sc_data->label, buf);
                lv_obj_align(sc_data->label, NULL, LV_ALIGN_CENTER, 0, 0);
            }
        }
    }
}
Beispiel #6
0
static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
{
	int res = 0;
	loff_t bmp_size;
	loff_t actread;
	char *splash_file;

	splash_file = env_get("splashfile");
	if (!splash_file)
		splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;

	if (location->storage == SPLASH_STORAGE_USB)
		res = splash_init_usb();

	if (location->storage == SPLASH_STORAGE_SATA)
		res = splash_init_sata();

	if (location->ubivol != NULL)
		res = splash_mount_ubifs(location);

	if (res)
		return res;

	res = splash_select_fs_dev(location);
	if (res)
		goto out;

	res = fs_size(splash_file, &bmp_size);
	if (res) {
		printf("Error (%d): cannot determine file size\n", res);
		goto out;
	}

	if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
		printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
		res = -EFAULT;
		goto out;
	}

	splash_select_fs_dev(location);
	res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);

out:
	if (location->ubivol != NULL)
		splash_umount_ubifs();

	return res;
}
Beispiel #7
0
static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
		efi_uintn_t pos)
{
	struct file_handle *fh = to_fh(file);
	efi_status_t ret = EFI_SUCCESS;

	EFI_ENTRY("%p, %zu", file, pos);

	if (fh->isdir) {
		if (pos != 0) {
			ret = EFI_UNSUPPORTED;
			goto error;
		}
		fs_closedir(fh->dirs);
		fh->dirs = NULL;
	}

	if (pos == ~0ULL) {
		loff_t file_size;

		if (set_blk_dev(fh)) {
			ret = EFI_DEVICE_ERROR;
			goto error;
		}

		if (fs_size(fh->path, &file_size)) {
			ret = EFI_DEVICE_ERROR;
			goto error;
		}

		pos = file_size;
	}

	fh->offset = pos;

error:
	return EFI_EXIT(ret);
}
Beispiel #8
0
void
menu_init()
{
    // Parse the MENU file, cache the menu offsets
    minode = fs_get_inode(&fs, "MENU");
    if(minode == 0xffff)
        return;
    menu_filelen = (uint16_t)fs_size(&fs, minode);
    uint16_t nextoffset, offset = 0;
    uint8_t menu_line[8], idx = 0;

    while((nextoffset = menu_get_line(offset,menu_line,sizeof(menu_line))) != 0) {
        if(menu_line[0] == 'M' || menu_line[0] == 'D') {    // Menu or Macro-def
            menu_offset[idx] = offset;
            if(idx < NMENUS)
                idx++;
        }
        offset = nextoffset;
    }
    menu_stackidx = 0;
    joyfunc = menu_handle_joystick;       // parse input
    menu_push(0);
}
Beispiel #9
0
block_off_t parity_used_size(struct snapraid_state* state)
{
	block_off_t parity_block;
	tommy_node* i;

	/* compute the size of the parity file */
	parity_block = 0;
	for (i = state->disklist; i != 0; i = i->next) {
		struct snapraid_disk* disk = i->data;

		/* start from the declared size */
		block_off_t block = fs_size(disk);

		/* decrease the block until an used one */
		while (block > parity_block && !block_has_file_and_valid_parity(fs_par2block_find(disk, block - 1)))
			--block;

		/* get the highest value */
		if (block > parity_block)
			parity_block = block;
	}

	return parity_block;
}
Beispiel #10
0
static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
					    const efi_guid_t *info_type,
					    efi_uintn_t *buffer_size,
					    void *buffer)
{
	struct file_handle *fh = to_fh(file);
	efi_status_t ret = EFI_SUCCESS;

	EFI_ENTRY("%p, %p, %p, %p", file, info_type, buffer_size, buffer);

	if (!guidcmp(info_type, &efi_file_info_guid)) {
		struct efi_file_info *info = buffer;
		char *filename = basename(fh);
		unsigned int required_size;
		loff_t file_size;

		/* check buffer size: */
		required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
		if (*buffer_size < required_size) {
			*buffer_size = required_size;
			ret = EFI_BUFFER_TOO_SMALL;
			goto error;
		}

		if (set_blk_dev(fh)) {
			ret = EFI_DEVICE_ERROR;
			goto error;
		}

		if (fs_size(fh->path, &file_size)) {
			ret = EFI_DEVICE_ERROR;
			goto error;
		}

		memset(info, 0, required_size);

		info->size = required_size;
		info->file_size = file_size;
		info->physical_size = file_size;

		if (fh->isdir)
			info->attribute |= EFI_FILE_DIRECTORY;

		ascii2unicode((u16 *)info->file_name, filename);
	} else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
		struct efi_file_system_info *info = buffer;
		disk_partition_t part;
		efi_uintn_t required_size;
		int r;

		if (fh->fs->part >= 1)
			r = part_get_info(fh->fs->desc, fh->fs->part, &part);
		else
			r = part_get_info_whole_disk(fh->fs->desc, &part);
		if (r < 0) {
			ret = EFI_DEVICE_ERROR;
			goto error;
		}
		required_size = sizeof(info) + 2 *
				(strlen((const char *)part.name) + 1);
		if (*buffer_size < required_size) {
			*buffer_size = required_size;
			ret = EFI_BUFFER_TOO_SMALL;
			goto error;
		}

		memset(info, 0, required_size);

		info->size = required_size;
		info->read_only = true;
		info->volume_size = part.size * part.blksz;
		info->free_space = 0;
		info->block_size = part.blksz;
		/*
		 * TODO: The volume label is not available in U-Boot.
		 * Use the partition name as substitute.
		 */
		ascii2unicode((u16 *)info->volume_label,
			      (const char *)part.name);
	} else {
		ret = EFI_UNSUPPORTED;
	}

error:
	return EFI_EXIT(ret);
}
Beispiel #11
0
FILE *fopen(const char *path, const char *mode) {
	uint64_t fd;
	uint8_t perm;
	FILE *stream;

	/* check mode */
	if (mode[0] != 'a' && mode[0] != 'w' && mode[0] != 'r') {
		errno = EINVAL;
		return NULL;
	}

	/* attempt to find the file */
	fd = io_find(path);

	/* check if the object is a directory or null */
	if (fd && (fs_type(fd) & RP_TYPE_FILE) == 0) {
		errno = EISDIR;
		return NULL;
	}

	if (!fd) {

		/* create the file */
		if (mode[0] == 'w' || mode[0] == 'a') {
			if (!io_cons(path, RP_TYPE_FILE)) {
				return NULL;
			}
		}
		else {
			errno = ENOENT;
			return NULL;
		}
	}

	perm = fs_perm(fd, gettuser());

	/* check read permissions */
	if (mode[0] == 'r' || mode[1] == '+') {
		if ((perm & PERM_READ) == 0) {
			errno = EACCES;
			return NULL;
		}
	}

	/* check write permissions */
	if (mode[0] == 'w' || mode[0] == 'a' || mode[1] == '+') {
		if ((perm & PERM_WRITE) == 0) {
			errno = EACCES;
			return NULL;
		}
	}

	/* reset the file contents */
	if (mode[0] == 'w') {
		reset(fd);
	}

	/* open a stream on the file */
	stream = malloc(sizeof(FILE));

	if (!stream) {
		errno = ENOMEM;
		return NULL;
	}

	stream->fd       = fd;
	stream->mutex    = false;
	stream->position = 0;
	stream->size     = fs_size(fd);
	stream->buffer   = NULL;
	stream->buffsize = 0;
	stream->buffpos  = 0;
	stream->revbuf   = EOF;
	stream->flags    = FILE_NBF | FILE_READ;

	if (mode[0] == 'w' || mode[0] == 'a' || mode[1] == '+') {
		stream->flags |= FILE_WRITE;
	}

	/* position the stream properly */
	if (mode[0] == 'a' && mode[1] != '+') {
		fseek(stream, 0, SEEK_END);
	}
	else {
		fseek(stream, 0, SEEK_SET);
	}

	return stream;
}
Beispiel #12
0
/**
 * Start the sending of a file
 * @param app pointer to a Files application
 * @param path path of the file to send
 */
static void start_send(lv_app_inst_t * app, const char * path)
{
    my_app_data_t * app_data = app->app_data;

    /*Open the file*/
    fs_res_t res = fs_open(&app_data->file, path, FS_MODE_RD);
    if(res == FS_RES_OK) {
        app_data->send_in_prog = 1;

        /*Send the header*/
        if(app_data->send_fn != 0) {
            lv_app_com_send(app, LV_APP_COM_TYPE_CHAR, app_data->path, strlen(app_data->path));
            lv_app_com_send(app, LV_APP_COM_TYPE_CHAR, "/", 1);
            lv_app_com_send(app, LV_APP_COM_TYPE_CHAR, app_data->fn, strlen(app_data->fn));
            lv_app_com_send(app, LV_APP_COM_TYPE_CHAR, "\n", 1);
        }

        if(app_data->send_size != 0) {
            char buf[64];
            uint32_t size;
            fs_size(&app_data->file, &size);
            sprintf(buf,"%d", (int) size);
            lv_app_com_send(app, LV_APP_COM_TYPE_CHAR, buf, strlen(buf));
            lv_app_com_send(app, LV_APP_COM_TYPE_CHAR, "\n", 1);
        }
        if(app_data->send_crc != 0) {
            lv_app_com_send(app, LV_APP_COM_TYPE_CHAR, "0x0000", 6);
            lv_app_com_send(app, LV_APP_COM_TYPE_CHAR, "\n", 1);
        }

        /*Add an extra \n to separate the header from the file data*/
        if(app_data->send_fn != 0 || app_data->send_size != 0 || app_data->send_crc != 0) {
            lv_app_com_send(app, LV_APP_COM_TYPE_CHAR, "\n", 1);
        } 
    }

    /*If an error occurred  close the file*/
    if(res != FS_RES_OK) {
        fs_close(&app_data->file);
        ptask_set_prio(app_data->send_task, PTASK_PRIO_OFF);
        app_data->send_in_prog = 0;
        lv_app_notice_add("Can not send\nthe file in Files");
    }
    /*If no error show notification, start the sender task and refresh the shortcut*/
    else {
       /*Start the sender task*/
       ptask_set_period(app_data->send_task, app_data->chunk_delay);
       ptask_reset(app_data->send_task);
       ptask_set_prio(app_data->send_task, PTASK_PRIO_HIGH);
       lv_app_notice_add("Sending\n%s", fs_get_last(path));

       /*Refresh the shortcut with the percentage of the sending*/
       if(app->sc_data != NULL) {
           my_sc_data_t * sc_data = app->sc_data;

           uint32_t size;
           fs_size(&app_data->file, &size);
           uint32_t pos;
           fs_tell(&app_data->file, &pos);

           int pct = (uint32_t) (pos * 100) / size;

           char buf[256];
           sprintf(buf, "Sending\n%d%%", pct);
           lv_label_set_text(sc_data->label, buf);
           lv_obj_align(sc_data->label, NULL, LV_ALIGN_CENTER, 0, 0);
       }
   }
}