Example #1
0
void fs_reset_specific(uint32_t addr_offset, uint32_t phys_addr, uint32_t phys_size,
    uint32_t phys_sector_size,
    uint32_t log_block_size, uint32_t log_page_size) {
  fs_set_addr_offset(addr_offset);
  memset(area, 0xcc, sizeof(area));
  memset(&AREA(phys_addr), 0xff, phys_size);
  memset(&__fs, 0, sizeof(__fs));

  memset(erases,0,sizeof(erases));
  memset(_cache,0,sizeof(_cache));

  int32_t res = fs_mount_specific(phys_addr, phys_size, phys_sector_size, log_block_size, log_page_size);

#if SPIFFS_USE_MAGIC
  if (res == SPIFFS_OK) {
    SPIFFS_unmount(&__fs);
  }
  res = SPIFFS_format(&__fs);
  if (res != SPIFFS_OK) {
    printf("format failed, %i\n", SPIFFS_errno(&__fs));
  }
  res = fs_mount_specific(phys_addr, phys_size, phys_sector_size, log_block_size, log_page_size);
  if (res != SPIFFS_OK) {
    printf("mount failed, %i\n", SPIFFS_errno(&__fs));
  }
#endif

  clear_flash_ops_log();
  log_flash_ops = 1;
  fs_check_fixes = 0;
}
Example #2
0
int ICACHE_FLASH_ATTR roffs_mount(uint32_t flashAddress)
{
    spiffs_config cfg;
    s32_t spiffs_hal_read(u32_t addr, u32_t size, u8_t *dst);
    s32_t spiffs_hal_write(u32_t addr, u32_t size, u8_t *src);
    s32_t spiffs_hal_erase(u32_t addr, u32_t size);
    cfg.hal_read_f = spiffs_hal_read;
    cfg.hal_write_f = spiffs_hal_write;
    cfg.hal_erase_f = spiffs_hal_erase;
    int res = SPIFFS_mount(&fs,
                           &cfg,
                           spiffs_work_buf,
                           spiffs_fds, sizeof(spiffs_fds),
                           NULL, 0,
                           NULL);
    if (res != SPIFFS_OK) {
        os_printf("Formatting flash filesystem\n");
        res = SPIFFS_format(&fs);
        if (res == SPIFFS_OK) {
            res = SPIFFS_mount(&fs,
                              &cfg,
                              spiffs_work_buf,
                              spiffs_fds, sizeof(spiffs_fds),
                              NULL, 0,
                              NULL);
        }
    }

    return res == SPIFFS_OK ? 0 : -1;
}
static int fs_format(int cidx) {
  s32_t r;
  struct mount_info *m = &s_fsm;
  _u32 fsc_size = FS_CONTAINER_SIZE(FS_SIZE);
  dprintf(("formatting %d s=%u, cs=%d\n", cidx, FS_SIZE, (int) fsc_size));

  m->cidx = cidx;
  r = fs_create_container(cidx, FS_SIZE);
  if (r < 0) goto out;
  m->fh = r;
  m->valid = m->rw = m->formatting = 1;
  /* Touch a byte at the end to open a "hole". */
  r = sl_FsWrite(m->fh, fsc_size - 1, (_u8 *) "\xff", 1);
  dprintf(("write 1 @ %d %d\n", (int) (fsc_size - 1), (int) r));
  if (r != 1) goto out_close;

  /* There must be a mount attempt before format. It'll fail and that's ok. */
  r = fs_mount_spiffs(m, FS_SIZE, FS_BLOCK_SIZE, FS_PAGE_SIZE);
  dprintf(("mount: %d\n", (int) r));
  r = SPIFFS_format(&m->fs);
  dprintf(("format: %d\n", (int) r));
  if (r != SPIFFS_OK) goto out_close;

  m->seq = INITIAL_SEQ;
  r = fs_write_meta(m);

out_close:
  sl_FsClose(m->fh, NULL, NULL, 0);
out:
  m->fh = -1;
  m->valid = m->formatting = m->rw = 0;
  return r;
}
Example #4
0
/**
 * addr_offset
 */
void fs_reset_specific(u32_t addr_offset, u32_t phys_addr, u32_t phys_size,
    u32_t phys_sector_size,
    u32_t log_block_size, u32_t log_page_size) {
  fs_create(phys_size + phys_addr - addr_offset,
            phys_sector_size,
            log_page_size,
            DEFAULT_NUM_FD,
            DEFAULT_NUM_CACHE_PAGES);
  fs_set_addr_offset(addr_offset);
  memset(&AREA(addr_offset), 0xcc, _area_sz);
  memset(&AREA(phys_addr), 0xff, phys_size);
  memset(&__fs, 0, sizeof(__fs));

  s32_t res = fs_mount_specific(phys_addr, phys_size, phys_sector_size, log_block_size, log_page_size);

#if SPIFFS_USE_MAGIC
  if (res == SPIFFS_OK) {
    SPIFFS_unmount(&__fs);
  }
  res = SPIFFS_format(&__fs);
  if (res != SPIFFS_OK) {
    printf("format failed, %i\n", SPIFFS_errno(&__fs));
  }
  res = fs_mount_specific(phys_addr, phys_size, phys_sector_size, log_block_size, log_page_size);
  if (res != SPIFFS_OK) {
    printf("mount failed, %i\n", SPIFFS_errno(&__fs));
  }
#endif

  clear_flash_ops_log();
  log_flash_ops = 1;
  fs_check_fixes = 0;
}
// FS formatting function
bool spiffs_format()
{
	if (_filesystemStorageHandle.user_data)
	{
		return SPIFFS_ERR_NOT_MOUNTED;
	}
	if (SPIFFS_mounted(&_filesystemStorageHandle))
	{
		return SPIFFS_ERR_MOUNTED;
	}
	SPIFFS_format(&_filesystemStorageHandle);

	return true;
}
Example #6
0
// FS formatting function
// Returns 1 if OK, 0 for error
int myspiffs_format( void )
{
  SPIFFS_unmount(&fs);
  myspiffs_mount_internal(TRUE);
  SPIFFS_unmount(&fs);

  NODE_DBG("Formatting: size 0x%x, addr 0x%x\n", fs.cfg.phys_size, fs.cfg.phys_addr);

  if (SPIFFS_format(&fs) < 0) {
    return 0;
  }

  return myspiffs_mount();
}
Example #7
0
//file.format()
static int file_format( lua_State* L )
{
  if(SPIFFS_mounted(&fs)==false) lua_spiffs_mount();
   
  SPIFFS_unmount(&fs);
  
  int ret = SPIFFS_format(&fs);
  if(ret==SPIFFS_OK)
  {
    l_message(NULL,"format done\r\n");
    lua_spiffs_mount();    
  }
  else
    l_message(NULL,"format error\r\n");
  return 0;
}
Example #8
0
File: fs.c Project: pellepl/wisleep
int32_t fs_mount(void) {
  int32_t res;
  const uint32_t fs_size =
      (sdk_flashchip.chip_size / 8 - SPI_FLASH_SEC_SIZE * FS_SLACK_END_SECTORS) & ~(FS_BLOCK_SZ-1);
  const uint32_t fs_addr =
      (sdk_flashchip.chip_size - fs_size - SPI_FLASH_SEC_SIZE * FS_SLACK_END_SECTORS)  & ~(FS_BLOCK_SZ-1);
  spiffs_config cfg = {
      .hal_read_f = _spiffs_hal_read,
      .hal_write_f = _spiffs_hal_write,
      .hal_erase_f = _spiffs_hal_erase,
      .phys_size = fs_size,
      .phys_addr = fs_addr,
      .phys_erase_block = SPI_FLASH_SEC_SIZE,
      .log_block_size = FS_BLOCK_SZ,
      .log_page_size = FS_PAGE_SZ,
      .fh_ix_offset = SPIFFS_FILEHDL_OFFSET_NUM
  };
  printf("mounting fs @ 0x%08x, %i kbytes\n", fs_addr, fs_size / 1024);
  res = SPIFFS_mount(FS,
      &cfg, (uint8_t *)_fs_work,
      (uint8_t *)_fs_desc, sizeof(_fs_desc),
      (uint8_t *)_fs_cache, sizeof(_fs_cache),
      _spiffs_check_cb_f);
  if (res != SPIFFS_OK && SPIFFS_errno(FS) == SPIFFS_ERR_NOT_A_FS) {
    printf("fs format\n");
    SPIFFS_clearerr(FS);
    res = SPIFFS_format(FS);
    if (res == SPIFFS_OK) {
      printf("remount\n");
      res = SPIFFS_mount(FS,
          &cfg, (uint8_t *)_fs_work,
          (uint8_t *)_fs_desc, sizeof(_fs_desc),
          (uint8_t *)_fs_cache, sizeof(_fs_cache),
          _spiffs_check_cb_f);
    }
  }
  if (res != SPIFFS_OK) {
    printf("err fs mount %i\n", res);
  } else {
    uint32_t total, used;
    SPIFFS_info(FS, &total, &used);
    printf("mounted fs: total %i kbytes, used %i kbytes\n", total / 1024, used / 1024);
  }
  printf("mount result:%i\n", SPIFFS_errno(FS));

  return res;
}
Example #9
0
int main(int argc, char **argv) {
  const char *root_dir;
  DIR *dir;

  if (argc < 3) {
    fprintf(stderr, "usage: %s <size> <root_dir>\n", argv[0]);
    return 1;
  }

  image_size = atoi(argv[1]);
  if (image_size == 0) {
    fprintf(stderr, "invalid size '%s'\n", argv[1]);
    return 1;
  }
  root_dir = argv[2];

  image = malloc(image_size);
  if (image == NULL) {
    fprintf(stderr, "cannot allocate %lu bytes\n", image_size);
    return 1;
  }

  mem_spiffs_erase(0, image_size);
  mem_spiffs_mount();  // Will fail but is required.
  SPIFFS_format(&fs);
  if (mem_spiffs_mount() != SPIFFS_OK) {
    fprintf(stderr, "SPIFFS_mount failed: %d\n", SPIFFS_errno(&fs));
    return 1;
  }

  fprintf(stderr, "adding files in directory %s\n", root_dir);
  if ((dir = opendir(root_dir)) == NULL) {
    fprintf(stderr, "unable to open directory %s\n", root_dir);
    return 1;
  } else {
    read_dir(dir, root_dir);
  }

  fwrite(image, image_size, 1, stdout);

  u32_t total, used;
  SPIFFS_info(&fs, &total, &used);
  fprintf(stderr, "Image stats: size=%u, space: total=%u, used=%u, free=%u\n",
          (unsigned int) image_size, total, used, total - used);

  return 0;
}
Example #10
0
bool spiffs_format_internal(spiffs_config *cfg)
{
	// should we use _filestorage handle
	SPIFFS_format(&_filesystemStorageHandle);
}
Example #11
0
static esp_err_t esp_spiffs_init(const esp_vfs_spiffs_conf_t* conf)
{
    int index;
    //find if such partition is already mounted
    if (esp_spiffs_by_label(conf->partition_label, &index) == ESP_OK) {
        return ESP_ERR_INVALID_STATE;
    }

    if (esp_spiffs_get_empty(&index) != ESP_OK) {
        ESP_LOGE(TAG, "max mounted partitions reached");
        return ESP_ERR_INVALID_STATE;
    }

    esp_partition_subtype_t subtype = conf->partition_label ?
            ESP_PARTITION_SUBTYPE_ANY : ESP_PARTITION_SUBTYPE_DATA_SPIFFS;
    const esp_partition_t* partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 
                                      subtype, conf->partition_label);
    if (!partition) {
        ESP_LOGE(TAG, "spiffs partition could not be found");
        return ESP_ERR_NOT_FOUND;
    }

    if (partition->encrypted) {
        ESP_LOGE(TAG, "spiffs can not run on encrypted partition");
        return ESP_ERR_INVALID_STATE;
    }

    esp_spiffs_t * efs = malloc(sizeof(esp_spiffs_t));
    if (efs == NULL) {
        ESP_LOGE(TAG, "esp_spiffs could not be malloced");
        return ESP_ERR_NO_MEM;
    }
    memset(efs, 0, sizeof(esp_spiffs_t));

    efs->cfg.hal_erase_f       = spiffs_api_erase;
    efs->cfg.hal_read_f        = spiffs_api_read;
    efs->cfg.hal_write_f       = spiffs_api_write;
    efs->cfg.log_block_size    = g_rom_flashchip.sector_size;
    efs->cfg.log_page_size     = g_rom_flashchip.page_size;
    efs->cfg.phys_addr         = 0;
    efs->cfg.phys_erase_block  = g_rom_flashchip.sector_size;
    efs->cfg.phys_size         = partition->size;

    efs->by_label = conf->partition_label != NULL;

    efs->lock = xSemaphoreCreateMutex();
    if (efs->lock == NULL) {
        ESP_LOGE(TAG, "mutex lock could not be created");
        esp_spiffs_free(&efs);
        return ESP_ERR_NO_MEM;
    }

    efs->fds_sz = conf->max_files * sizeof(spiffs_fd);
    efs->fds = malloc(efs->fds_sz);
    if (efs->fds == NULL) {
        ESP_LOGE(TAG, "fd buffer could not be malloced");
        esp_spiffs_free(&efs);
        return ESP_ERR_NO_MEM;
    }
    memset(efs->fds, 0, efs->fds_sz);

#if SPIFFS_CACHE
    efs->cache_sz = sizeof(spiffs_cache) + conf->max_files * (sizeof(spiffs_cache_page)
                          + efs->cfg.log_page_size);
    efs->cache = malloc(efs->cache_sz);
    if (efs->cache == NULL) {
        ESP_LOGE(TAG, "cache buffer could not be malloced");
        esp_spiffs_free(&efs);
        return ESP_ERR_NO_MEM;
    }
    memset(efs->cache, 0, efs->cache_sz);
#endif

    const uint32_t work_sz = efs->cfg.log_page_size * 2;
    efs->work = malloc(work_sz);
    if (efs->work == NULL) {
        ESP_LOGE(TAG, "work buffer could not be malloced");
        esp_spiffs_free(&efs);
        return ESP_ERR_NO_MEM;
    }
    memset(efs->work, 0, work_sz);

    efs->fs = malloc(sizeof(spiffs));
    if (efs->fs == NULL) {
        ESP_LOGE(TAG, "spiffs could not be malloced");
        esp_spiffs_free(&efs);
        return ESP_ERR_NO_MEM;
    }
    memset(efs->fs, 0, sizeof(spiffs));

    efs->fs->user_data = (void *)efs;
    efs->partition = partition;

    s32_t res = SPIFFS_mount(efs->fs, &efs->cfg, efs->work, efs->fds, efs->fds_sz, 
                            efs->cache, efs->cache_sz, spiffs_api_check);

    if (conf->format_if_mount_failed && res != SPIFFS_OK) {
        ESP_LOGW(TAG, "mount failed, %i. formatting...", SPIFFS_errno(efs->fs));
        SPIFFS_clearerr(efs->fs);
        res = SPIFFS_format(efs->fs);
        if (res != SPIFFS_OK) {
            ESP_LOGE(TAG, "format failed, %i", SPIFFS_errno(efs->fs));
            SPIFFS_clearerr(efs->fs);
            esp_spiffs_free(&efs);
            return ESP_FAIL;
        }
        res = SPIFFS_mount(efs->fs, &efs->cfg, efs->work, efs->fds, efs->fds_sz, 
                            efs->cache, efs->cache_sz, spiffs_api_check);
    }
    if (res != SPIFFS_OK) {
        ESP_LOGE(TAG, "mount failed, %i", SPIFFS_errno(efs->fs));
        SPIFFS_clearerr(efs->fs);
        esp_spiffs_free(&efs);
        return ESP_FAIL;
    }
    _efs[index] = efs;
    return ESP_OK;
}