const esp_phy_init_data_t* esp_phy_get_init_data() { const esp_partition_t* partition = esp_partition_find_first( ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_PHY, NULL); if (partition == NULL) { ESP_LOGE(TAG, "PHY data partition not found"); return NULL; } ESP_LOGD(TAG, "loading PHY init data from partition at offset 0x%x", partition->address); size_t init_data_store_length = sizeof(phy_init_magic_pre) + sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post); uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length); if (init_data_store == NULL) { ESP_LOGE(TAG, "failed to allocate memory for PHY init data"); return NULL; } esp_err_t err = esp_partition_read(partition, 0, init_data_store, init_data_store_length); if (err != ESP_OK) { ESP_LOGE(TAG, "failed to read PHY data partition (0x%x)", err); return NULL; } if (memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) != 0 || memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post), PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) != 0) { ESP_LOGE(TAG, "failed to validate PHY data partition"); return NULL; } ESP_LOGD(TAG, "PHY data partition validated"); return (const esp_phy_init_data_t*) (init_data_store + sizeof(phy_init_magic_pre)); }
static bool esp32_vfs_dev_partition_open(struct mgos_vfs_dev *dev, const char *opts) { char *label = NULL; int subtype = 0xff; /* any subtype */ json_scanf(opts, strlen(opts), "{name: %Q, label: %Q, subtype: %d}", &label, &label, &subtype); if (label == NULL) { LOG(LL_ERROR, ("Must specify partition label")); return false; } const esp_partition_t *part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, subtype, label); if (part != NULL) { dev->dev_data = (void *) part; } free(label); return (part != NULL); }
esp_err_t esp_vfs_fat_spiflash_mount(const char* base_path, const char* partition_label, const esp_vfs_fat_mount_config_t* mount_config, wl_handle_t* wl_handle) { esp_err_t result = ESP_OK; const size_t workbuf_size = 4096; void *workbuf = NULL; esp_partition_subtype_t subtype = partition_label ? ESP_PARTITION_SUBTYPE_ANY : ESP_PARTITION_SUBTYPE_DATA_FAT; const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, subtype, partition_label); if (data_partition == NULL) { ESP_LOGE(TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label); return ESP_ERR_NOT_FOUND; } result = wl_mount(data_partition, wl_handle); if (result != ESP_OK) { ESP_LOGE(TAG, "failed to mount wear levelling layer. result = %i", result); return result; } // connect driver to FATFS BYTE pdrv = 0xFF; if (ff_diskio_get_drive(&pdrv) != ESP_OK) { ESP_LOGD(TAG, "the maximum count of volumes is already mounted"); return ESP_ERR_NO_MEM; } ESP_LOGD(TAG, "using pdrv=%i", pdrv); char drv[3] = {(char)('0' + pdrv), ':', 0}; result = ff_diskio_register_wl_partition(pdrv, *wl_handle); if (result != ESP_OK) { ESP_LOGE(TAG, "ff_diskio_register_wl_partition failed pdrv=%i, error - 0x(%x)", pdrv, result); goto fail; } FATFS *fs; result = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs); if (result == ESP_ERR_INVALID_STATE) { // it's okay, already registered with VFS } else if (result != ESP_OK) { ESP_LOGD(TAG, "esp_vfs_fat_register failed 0x(%x)", result); goto fail; } // Try to mount partition FRESULT fresult = f_mount(fs, drv, 1); if (fresult != FR_OK) { ESP_LOGW(TAG, "f_mount failed (%d)", fresult); if (!(fresult == FR_NO_FILESYSTEM && mount_config->format_if_mount_failed)) { result = ESP_FAIL; goto fail; } workbuf = malloc(workbuf_size); ESP_LOGI(TAG, "Formatting FATFS partition"); fresult = f_mkfs(drv, FM_ANY | FM_SFD, workbuf_size, workbuf, workbuf_size); if (fresult != FR_OK) { result = ESP_FAIL; ESP_LOGE(TAG, "f_mkfs failed (%d)", fresult); goto fail; } free(workbuf); workbuf = NULL; ESP_LOGI(TAG, "Mounting again"); fresult = f_mount(fs, drv, 0); if (fresult != FR_OK) { result = ESP_FAIL; ESP_LOGE(TAG, "f_mount failed after formatting (%d)", fresult); goto fail; } } return ESP_OK; fail: free(workbuf); esp_vfs_fat_unregister_path(base_path); ff_diskio_unregister(pdrv); return result; }
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; }