| /* |
| * Copyright (c) 2016 Intel Corporation. |
| * Copyright (c) 2021,2023 Nordic Semiconductor ASA |
| * |
| * SPDX-License-Identifier: Apache-2.0 |
| */ |
| |
| #include <string.h> |
| #include <zephyr/types.h> |
| #include <zephyr/drivers/disk.h> |
| #include <errno.h> |
| #include <zephyr/init.h> |
| #include <zephyr/device.h> |
| #include <zephyr/logging/log.h> |
| |
| LOG_MODULE_REGISTER(ramdisk, CONFIG_RAMDISK_LOG_LEVEL); |
| |
| #if defined(CONFIG_DISK_RAM_DECOMPRESS) |
| #include <lz4frame.h> |
| #define LZ4_MAGIC 0x184D2204 |
| #endif |
| |
| struct ram_disk_data { |
| struct disk_info *info; |
| #if defined(CONFIG_MMU) || defined(CONFIG_DISK_RAM_DECOMPRESS) |
| uint8_t *ramdisk_buf; |
| #endif /* CONFIG_MMU || CONFIG_DISK_RAM_DECOMPRESS */ |
| }; |
| |
| struct ram_disk_config { |
| const size_t sector_size; |
| const size_t sector_count; |
| const size_t size; |
| uint8_t *const buf; |
| #if defined(CONFIG_DISK_RAM_DECOMPRESS) |
| uint8_t *unpacked_buf; /* The buffer for decompressing */ |
| #endif /* CONFIG_DISK_RAM_DECOMPRESS */ |
| }; |
| |
| #if defined(CONFIG_MMU) |
| static uint8_t *disk_ram_external_map(const struct ram_disk_config *conf) |
| { |
| uint8_t *virt_start; |
| |
| k_mem_map_phys_bare(&virt_start, POINTER_TO_UINT(conf->buf), |
| conf->size, K_MEM_CACHE_WB | K_MEM_PERM_RW); |
| |
| return virt_start; |
| } |
| |
| #if defined(CONFIG_DISK_RAM_DECOMPRESS) |
| static void disk_ram_external_unmap(char *virt_start, size_t size) |
| { |
| k_mem_unmap_phys_bare(virt_start, size); |
| } |
| #endif /* CONFIG_DISK_RAM_DECOMPRESS */ |
| #endif /* CONFIG_MMU */ |
| |
| #if defined(CONFIG_DISK_RAM_DECOMPRESS) |
| static int disk_ram_is_compressed(char *virt_start) |
| { |
| uint32_t magic = *(uint32_t *)virt_start; |
| |
| return magic == LZ4_MAGIC; |
| } |
| |
| static int disk_ram_decompress(char *compressed_buf, size_t compressed_size, |
| char *decompressed_buf, size_t decompressed_size) |
| { |
| LZ4F_decompressionContext_t lz4_ctx = {0}; |
| int final_status = 0; |
| size_t ret; |
| |
| ret = LZ4F_createDecompressionContext(&lz4_ctx, LZ4F_VERSION); |
| if (LZ4F_isError(ret)) { |
| LOG_ERR("Can't create decompression context, error: %s\n", |
| LZ4F_getErrorName(ret)); |
| return ret; |
| } |
| |
| ret = LZ4F_decompress(lz4_ctx, decompressed_buf, &decompressed_size, |
| compressed_buf, &compressed_size, NULL); |
| if (LZ4F_isError(ret)) { |
| LOG_ERR("Decompression failed: %s\n", LZ4F_getErrorName(ret)); |
| final_status = ret; |
| goto cleanup; |
| } |
| |
| /* The frame is not fully decompressed */ |
| if (ret > 0) { |
| LOG_ERR("Decompression incomplete. Next expected size: %zu\n", ret); |
| final_status = -EMSGSIZE; |
| } |
| |
| cleanup: |
| LZ4F_freeDecompressionContext(lz4_ctx); |
| |
| return final_status; |
| } |
| #endif /* CONFIG_DISK_RAM_DECOMPRESS */ |
| |
| static void *lba_to_address(const struct device *dev, uint32_t lba) |
| { |
| const struct ram_disk_config *config = dev->config; |
| #if defined(CONFIG_MMU) || defined(CONFIG_DISK_RAM_DECOMPRESS) |
| struct ram_disk_data *data = dev->data; |
| uint8_t *ramdisk_buf = data->ramdisk_buf; |
| #else /* CONFIG_MMU || CONFIG_DISK_RAM_DECOMPRESS */ |
| uint8_t *ramdisk_buf = config->buf; |
| #endif /* CONFIG_MMU || CONFIG_DISK_RAM_DECOMPRESS */ |
| |
| return &ramdisk_buf[lba * config->sector_size]; |
| } |
| |
| static int disk_ram_access_status(struct disk_info *disk) |
| { |
| return DISK_STATUS_OK; |
| } |
| |
| static int disk_ram_access_read(struct disk_info *disk, uint8_t *buff, |
| uint32_t sector, uint32_t count) |
| { |
| const struct device *dev = disk->dev; |
| const struct ram_disk_config *config = dev->config; |
| uint32_t last_sector = sector + count; |
| |
| if (last_sector < sector || last_sector > config->sector_count) { |
| LOG_ERR("Sector %" PRIu32 " is outside the range %zu", |
| last_sector, config->sector_count); |
| return -EIO; |
| } |
| |
| memcpy(buff, lba_to_address(dev, sector), count * config->sector_size); |
| |
| return 0; |
| } |
| |
| static int disk_ram_access_write(struct disk_info *disk, const uint8_t *buff, |
| uint32_t sector, uint32_t count) |
| { |
| const struct device *dev = disk->dev; |
| const struct ram_disk_config *config = dev->config; |
| uint32_t last_sector = sector + count; |
| |
| if (last_sector < sector || last_sector > config->sector_count) { |
| LOG_ERR("Sector %" PRIu32 " is outside the range %zu", |
| last_sector, config->sector_count); |
| return -EIO; |
| } |
| |
| memcpy(lba_to_address(dev, sector), buff, count * config->sector_size); |
| |
| return 0; |
| } |
| |
| static int disk_ram_access_erase(struct disk_info *disk, uint32_t sector, |
| uint32_t count) |
| { |
| const struct device *dev = disk->dev; |
| const struct ram_disk_config *config = dev->config; |
| uint32_t last_sector = sector + count; |
| |
| if (last_sector < sector || last_sector > config->sector_count) { |
| LOG_ERR("Sector %" PRIu32 " is outside the range %zu", |
| last_sector, config->sector_count); |
| return -EINVAL; |
| } |
| |
| memset(lba_to_address(dev, sector), 0, count * config->sector_size); |
| |
| return 0; |
| } |
| |
| static int disk_ram_access_ioctl(struct disk_info *disk, uint8_t cmd, void *buff) |
| { |
| const struct ram_disk_config *config = disk->dev->config; |
| |
| switch (cmd) { |
| case DISK_IOCTL_CTRL_SYNC: |
| break; |
| case DISK_IOCTL_GET_SECTOR_COUNT: |
| *(uint32_t *)buff = config->sector_count; |
| break; |
| case DISK_IOCTL_GET_SECTOR_SIZE: |
| *(uint32_t *)buff = config->sector_size; |
| break; |
| case DISK_IOCTL_GET_ERASE_BLOCK_SZ: |
| *(uint32_t *)buff = 1U; |
| break; |
| case DISK_IOCTL_CTRL_INIT: |
| case DISK_IOCTL_CTRL_DEINIT: |
| break; |
| default: |
| return -EINVAL; |
| } |
| |
| return 0; |
| } |
| |
| static int disk_ram_access_init(struct disk_info *disk) |
| { |
| return disk_ram_access_ioctl(disk, DISK_IOCTL_CTRL_INIT, NULL); |
| } |
| |
| static int disk_ram_init(const struct device *dev) |
| { |
| struct ram_disk_data *data = dev->data; |
| struct disk_info *info = data->info; |
| |
| info->dev = dev; |
| |
| #if defined(CONFIG_MMU) |
| const struct ram_disk_config *config = dev->config; |
| |
| data->ramdisk_buf = disk_ram_external_map(config); |
| #elif defined(CONFIG_DISK_RAM_DECOMPRESS) |
| const struct ram_disk_config *config = dev->config; |
| |
| data->ramdisk_buf = config->buf; |
| #endif |
| |
| #if defined(CONFIG_DISK_RAM_DECOMPRESS) |
| if (disk_ram_is_compressed((char *)data->ramdisk_buf)) { |
| if (disk_ram_decompress((char *)data->ramdisk_buf, config->size, |
| (char *)config->unpacked_buf, config->size) < 0) { |
| return -EINVAL; |
| } |
| #if defined(CONFIG_MMU) |
| LOG_INF("Freeing compressed initrd"); |
| disk_ram_external_unmap(data->ramdisk_buf, config->size); |
| #endif /* CONFIG_MMU */ |
| data->ramdisk_buf = config->unpacked_buf; |
| } |
| #endif /* CONFIG_DISK_RAM_DECOMPRESS */ |
| |
| return disk_access_register(info); |
| } |
| |
| static const struct disk_operations ram_disk_ops = { |
| .init = disk_ram_access_init, |
| .status = disk_ram_access_status, |
| .read = disk_ram_access_read, |
| .write = disk_ram_access_write, |
| .erase = disk_ram_access_erase, |
| .ioctl = disk_ram_access_ioctl, |
| }; |
| |
| #define DT_DRV_COMPAT zephyr_ram_disk |
| |
| #define RAMDISK_DEVICE_SIZE(n) \ |
| (DT_INST_PROP(n, sector_size) * DT_INST_PROP(n, sector_count)) |
| |
| #define RAMDISK_DEVICE_CONFIG_DEFINE_MEMREG(n) \ |
| BUILD_ASSERT(RAMDISK_DEVICE_SIZE(n) <= \ |
| DT_REG_SIZE(DT_INST_PHANDLE(n, ram_region)), \ |
| "Disk size is smaller than memory region"); \ |
| \ |
| IF_ENABLED(CONFIG_DISK_RAM_DECOMPRESS, \ |
| (static uint8_t unpacked_buf_##n[RAMDISK_DEVICE_SIZE(n)];) \ |
| ) \ |
| \ |
| static struct ram_disk_config disk_config_##n = { \ |
| .sector_size = DT_INST_PROP(n, sector_size), \ |
| .sector_count = DT_INST_PROP(n, sector_count), \ |
| .size = RAMDISK_DEVICE_SIZE(n), \ |
| .buf = UINT_TO_POINTER(DT_REG_ADDR(DT_INST_PHANDLE(n, ram_region))), \ |
| IF_ENABLED(CONFIG_DISK_RAM_DECOMPRESS, \ |
| (.unpacked_buf = unpacked_buf_##n,) \ |
| ) \ |
| } |
| |
| #define RAMDISK_DEVICE_CONFIG_DEFINE_LOCAL(n) \ |
| static uint8_t disk_buf_##n[DT_INST_PROP(n, sector_size) * \ |
| DT_INST_PROP(n, sector_count)]; \ |
| \ |
| IF_ENABLED(CONFIG_DISK_RAM_DECOMPRESS, \ |
| (static uint8_t unpacked_buf_##n[RAMDISK_DEVICE_SIZE(n)];) \ |
| ) \ |
| \ |
| static struct ram_disk_config disk_config_##n = { \ |
| .sector_size = DT_INST_PROP(n, sector_size), \ |
| .sector_count = DT_INST_PROP(n, sector_count), \ |
| .size = RAMDISK_DEVICE_SIZE(n), \ |
| .buf = disk_buf_##n, \ |
| IF_ENABLED(CONFIG_DISK_RAM_DECOMPRESS, \ |
| (.unpacked_buf = unpacked_buf_##n,) \ |
| ) \ |
| } |
| |
| #define RAMDISK_DEVICE_CONFIG_DEFINE(n) \ |
| COND_CODE_1(DT_INST_NODE_HAS_PROP(n, ram_region), \ |
| (RAMDISK_DEVICE_CONFIG_DEFINE_MEMREG(n)), \ |
| (RAMDISK_DEVICE_CONFIG_DEFINE_LOCAL(n))) |
| |
| #define RAMDISK_DEVICE_DEFINE(n) \ |
| \ |
| static struct disk_info disk_info_##n = { \ |
| .name = DT_INST_PROP(n, disk_name), \ |
| .ops = &ram_disk_ops, \ |
| }; \ |
| \ |
| static struct ram_disk_data ram_disk_data_##n = { \ |
| .info = &disk_info_##n, \ |
| }; \ |
| \ |
| RAMDISK_DEVICE_CONFIG_DEFINE(n); \ |
| \ |
| DEVICE_DT_INST_DEFINE(n, disk_ram_init, NULL, \ |
| &ram_disk_data_##n, &disk_config_##n, \ |
| POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \ |
| &ram_disk_ops); |
| |
| DT_INST_FOREACH_STATUS_OKAY(RAMDISK_DEVICE_DEFINE) |