|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright 2026 Renesas, Embedd |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +#include <zephyr/kernel.h> |
| 9 | +#include <zephyr/usb/class/usbd_msc.h> |
| 10 | +#include <zephyr/drivers/disk.h> |
| 11 | +#include <zephyr/storage/disk_access.h> |
| 12 | + |
| 13 | +#include "fault_disk.h" |
| 14 | + |
| 15 | +#define FAULT_DISK_SECTOR_SIZE 512u |
| 16 | +#define FAULT_DISK_SECTOR_COUNT 64u |
| 17 | + |
| 18 | +/* |
| 19 | + * `USBD_DEFINE_MSC_LUN(id, ...)` places its LUN in the `usbd_msc_lun` iterable section, which |
| 20 | + * the linker sorts alphabetically by the resulting `usbd_msc_lun_##id` symbol name (SORT_BY_NAME, |
| 21 | + * see include/zephyr/linker/iterable_sections.h) -- NOT by declaration/link order. This id is |
| 22 | + * chosen to sort after ram_disk.c's "ram0"/"ram1" so this LUN lands last (index CONFIG_TEST_NUM_LUN |
| 23 | + * - 1, i.e. "USB0_2"), matching DISK_DRIVE_NAME_FAULT in msc.c. |
| 24 | + */ |
| 25 | +USBD_DEFINE_MSC_LUN(zzz_fault, FAULT_DISK_NAME, "Zephyr", "FaultDisk", "0.00"); |
| 26 | + |
| 27 | +static uint8_t backing[FAULT_DISK_SECTOR_COUNT][FAULT_DISK_SECTOR_SIZE]; |
| 28 | + |
| 29 | +/* -1: report the real status; otherwise the DISK_STATUS_* value to report instead */ |
| 30 | +static int status_override = -1; |
| 31 | +static bool read_should_fail; |
| 32 | +static bool write_should_fail; |
| 33 | + |
| 34 | +static int fault_disk_init(struct disk_info *disk) |
| 35 | +{ |
| 36 | + ARG_UNUSED(disk); |
| 37 | + |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
| 41 | +static int fault_disk_status(struct disk_info *disk) |
| 42 | +{ |
| 43 | + ARG_UNUSED(disk); |
| 44 | + |
| 45 | + return status_override >= 0 ? status_override : DISK_STATUS_OK; |
| 46 | +} |
| 47 | + |
| 48 | +static int fault_disk_read(struct disk_info *disk, uint8_t *data_buf, uint32_t start_sector, |
| 49 | + uint32_t num_sector) |
| 50 | +{ |
| 51 | + ARG_UNUSED(disk); |
| 52 | + |
| 53 | + if (read_should_fail) { |
| 54 | + return -EIO; |
| 55 | + } |
| 56 | + |
| 57 | + if (start_sector + num_sector > FAULT_DISK_SECTOR_COUNT) { |
| 58 | + return -EINVAL; |
| 59 | + } |
| 60 | + |
| 61 | + memcpy(data_buf, backing[start_sector], (size_t)num_sector * FAULT_DISK_SECTOR_SIZE); |
| 62 | + |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +static int fault_disk_write(struct disk_info *disk, const uint8_t *data_buf, uint32_t start_sector, |
| 67 | + uint32_t num_sector) |
| 68 | +{ |
| 69 | + ARG_UNUSED(disk); |
| 70 | + |
| 71 | + if (write_should_fail) { |
| 72 | + return -EIO; |
| 73 | + } |
| 74 | + |
| 75 | + if (start_sector + num_sector > FAULT_DISK_SECTOR_COUNT) { |
| 76 | + return -EINVAL; |
| 77 | + } |
| 78 | + |
| 79 | + memcpy(backing[start_sector], data_buf, (size_t)num_sector * FAULT_DISK_SECTOR_SIZE); |
| 80 | + |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
| 84 | +static int fault_disk_ioctl(struct disk_info *disk, uint8_t cmd, void *buff) |
| 85 | +{ |
| 86 | + ARG_UNUSED(disk); |
| 87 | + |
| 88 | + switch (cmd) { |
| 89 | + case DISK_IOCTL_GET_SECTOR_COUNT: |
| 90 | + *(uint32_t *)buff = FAULT_DISK_SECTOR_COUNT; |
| 91 | + return 0; |
| 92 | + case DISK_IOCTL_GET_SECTOR_SIZE: |
| 93 | + *(uint32_t *)buff = FAULT_DISK_SECTOR_SIZE; |
| 94 | + return 0; |
| 95 | + case DISK_IOCTL_CTRL_SYNC: |
| 96 | + case DISK_IOCTL_CTRL_INIT: |
| 97 | + case DISK_IOCTL_CTRL_DEINIT: |
| 98 | + return 0; |
| 99 | + default: |
| 100 | + return -ENOTSUP; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +static const struct disk_operations fault_disk_ops = { |
| 105 | + .init = fault_disk_init, |
| 106 | + .status = fault_disk_status, |
| 107 | + .read = fault_disk_read, |
| 108 | + .write = fault_disk_write, |
| 109 | + .ioctl = fault_disk_ioctl, |
| 110 | +}; |
| 111 | + |
| 112 | +static struct disk_info fault_disk_info = { |
| 113 | + .name = FAULT_DISK_NAME, |
| 114 | + .ops = &fault_disk_ops, |
| 115 | +}; |
| 116 | + |
| 117 | +void fault_disk_setup(void) |
| 118 | +{ |
| 119 | + disk_access_register(&fault_disk_info); |
| 120 | +} |
| 121 | + |
| 122 | +void fault_disk_reset(void) |
| 123 | +{ |
| 124 | + status_override = -1; |
| 125 | + read_should_fail = false; |
| 126 | + write_should_fail = false; |
| 127 | +} |
| 128 | + |
| 129 | +void fault_disk_set_status_override(int status) |
| 130 | +{ |
| 131 | + status_override = status; |
| 132 | +} |
| 133 | + |
| 134 | +void fault_disk_set_read_error(bool fail) |
| 135 | +{ |
| 136 | + read_should_fail = fail; |
| 137 | +} |
| 138 | + |
| 139 | +void fault_disk_set_write_error(bool fail) |
| 140 | +{ |
| 141 | + write_should_fail = fail; |
| 142 | +} |
0 commit comments