From bd6723678836804a2419cb51fcfa46a04c995d98 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Tue, 11 Apr 2023 18:38:21 +0200 Subject: [PATCH 01/13] drivers/mtd: store MTD pointers as XFA --- drivers/include/mtd.h | 52 +++++++++++++++++++++++++++++++++++++++++++ drivers/mtd/mtd.c | 4 ++++ 2 files changed, 56 insertions(+) diff --git a/drivers/include/mtd.h b/drivers/include/mtd.h index bc2ccafac5..320ec581bb 100644 --- a/drivers/include/mtd.h +++ b/drivers/include/mtd.h @@ -75,6 +75,8 @@ #include +#include "xfa.h" + #ifdef __cplusplus extern "C" { #endif @@ -116,6 +118,56 @@ typedef struct { #endif } mtd_dev_t; +/** + * @brief MTD device array as XFA + * + * The array contains the addresses of all MTD devices that are defined using + * the @ref MTD_XFA_ADD macro, for example: + * ``` + * MTD_XFA_ADD(my_dev, 0); + * ``` + * The MTD devices in this array can be used for automatic functions such as + * with the `mtd_default` module. The i-th device in this array can then be + * accessed with `mtd_dev_xfa[i]`. The number of MTDs defined in this array + * is `XFA_LEN(mtd_dev_xfa)`. + */ +#if !DOXYGEN +XFA_USE_CONST(mtd_dev_t *, mtd_dev_xfa); +#else +mtd_dev_t * const mtd_dev_xfa[]; +#endif + +/** + * @brief Define MTD device pointer variable `mtd` + * + * The macro defines the MTD device pointer variable `mtd`, sets it to + * the address of the MTD device specified by the @p dev parameter, and adds + * it to the XFA of MTD device pointers @ref mtd_dev_xfa. For example + * ``` + * MTD_XFA_ADD(my_dev, 1); + * ``` + * defines the variable `mtd1` pointing to the device `my_dev`. + * + * The parameter @p idx is used as priority of the MTD device pointer within + * the XFA. That means it determines the order of the MTD device pointers + * within @ref mtd_dev_xfa. + * + * @note Only if each MTD device is added with a unique priority and only if the + * priorities start at 0 and are used in consecutive order, the parameter + * @p idx corresponds to the position of the MTD device pointer within + * the @ref mtd_dev_xfa XFA and `mtd_dev_xfa[i]` points to the i-th MTD + * device. + * + * @param dev MTD device + * @param idx Priority of the MTD device pointer within the XFA + */ +#define MTD_XFA_ADD(dev, idx) XFA_CONST(mtd_dev_xfa, 0) mtd_dev_t *mtd ## idx = (mtd_dev_t *)&(dev) + +/** + * @brief Number of MTDs defined in the MTD device array in XFA + */ +#define MTD_NUMOF XFA_LEN(mtd_dev_t *, mtd_dev_xfa) + /** * @brief MTD driver can write any data to the storage without erasing it first. * diff --git a/drivers/mtd/mtd.c b/drivers/mtd/mtd.c index ac4570bdc9..f9a6bec4b4 100644 --- a/drivers/mtd/mtd.c +++ b/drivers/mtd/mtd.c @@ -28,6 +28,10 @@ #include "bitarithm.h" #include "mtd.h" +#include "xfa.h" + +/* Automatic MTD handling */ +XFA_INIT_CONST(mtd_dev_t *, mtd_dev_xfa); static bool out_of_bounds(mtd_dev_t *mtd, uint32_t page, uint32_t offset, uint32_t len) { From 739cb53ca95cb5d169e016af32112ab23a66f9c1 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Tue, 11 Apr 2023 18:47:59 +0200 Subject: [PATCH 02/13] drivers/mtd_default: use XFA with MTD pointers --- drivers/include/mtd_default.h | 74 +++++++---------------------------- 1 file changed, 14 insertions(+), 60 deletions(-) diff --git a/drivers/include/mtd_default.h b/drivers/include/mtd_default.h index 6ad168c8c6..671218635a 100644 --- a/drivers/include/mtd_default.h +++ b/drivers/include/mtd_default.h @@ -33,54 +33,28 @@ extern "C" { #include "mtd_emulated.h" #endif -#if !defined(MTD_NUMOF) && !DOXYGEN - -#if defined(MTD_3) -#define MTD_BOARD_NUMOF 4 -#elif defined(MTD_2) -#define MTD_BOARD_NUMOF 3 -#elif defined(MTD_1) -#define MTD_BOARD_NUMOF 2 -#elif defined(MTD_0) -#define MTD_BOARD_NUMOF 1 -#else -#define MTD_BOARD_NUMOF 0 -#endif - -#define MTD_SDCARD_NUMOF IS_USED(MODULE_MTD_SDCARD_DEFAULT) -#define MTD_EMULATED_NUMOF IS_USED(MODULE_MTD_EMULATED) - -/** - * @brief Number of MTD devices - */ -#define MTD_NUMOF (MTD_BOARD_NUMOF + MTD_SDCARD_NUMOF + MTD_EMULATED_NUMOF) - -#else -#define MTD_BOARD_NUMOF MTD_NUMOF -#endif /* !defined(MTD_NUMOF) && !DOXYGEN */ - #if !DOXYGEN /** - * @brief Declare `mtd*` according to the number of MTD devices + * @brief Declare `mtd*` according to the `MTD_*` symbols defined by the board */ -#if MTD_NUMOF > 0 -extern mtd_dev_t *mtd0; +#ifdef MTD_0 +extern mtd_dev_t *MTD_0; #endif -#if MTD_NUMOF > 1 -extern mtd_dev_t *mtd1; +#ifdef MTD_1 +extern mtd_dev_t *MTD_1; #endif -#if MTD_NUMOF > 2 -extern mtd_dev_t *mtd2; +#ifdef MTD_2 +extern mtd_dev_t *MTD_2; #endif -#if MTD_NUMOF > 3 -extern mtd_dev_t *mtd3; +#ifdef MTD_3 +extern mtd_dev_t *MTD_3; #endif -#if MTD_NUMOF > 4 -extern mtd_dev_t *mtd4; +#ifdef MTD_4 +extern mtd_dev_t *MTD_4; #endif -#if MTD_NUMOF > 5 -extern mtd_dev_t *mtd5; +#ifdef MTD_5 +extern mtd_dev_t *MTD_5; #endif #endif /* !DOXYGEN */ @@ -102,27 +76,7 @@ extern mtd_emulated_t mtd_emulated_dev0; */ static inline mtd_dev_t *mtd_default_get_dev(unsigned idx) { - switch (idx) { -#if MTD_BOARD_NUMOF > 0 - case 0: return MTD_0; -#endif -#if MTD_BOARD_NUMOF > 1 - case 1: return MTD_1; -#endif -#if MTD_BOARD_NUMOF > 2 - case 2: return MTD_2; -#endif -#if MTD_BOARD_NUMOF > 3 - case 3: return MTD_3; -#endif -#if MTD_SDCARD_NUMOF > 0 - case MTD_BOARD_NUMOF: return (mtd_dev_t *)&mtd_sdcard_dev0; -#endif -#if MTD_EMULATED_NUMOF > 0 - case MTD_BOARD_NUMOF + MTD_SDCARD_NUMOF: return (mtd_dev_t *)&mtd_emulated_dev0; -#endif - } - return NULL; + return ((MTD_NUMOF != 0) && (idx < MTD_NUMOF)) ? mtd_dev_xfa[idx] : NULL; } #ifdef __cplusplus From e5c541b77c8d956f868cd07188e310d332ae7088 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Tue, 11 Apr 2023 18:40:00 +0200 Subject: [PATCH 03/13] drivers/mtd_sdcard: use XFA with MTD pointers --- drivers/mtd_sdcard/mtd_sdcard.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/mtd_sdcard/mtd_sdcard.c b/drivers/mtd_sdcard/mtd_sdcard.c index 39136723dd..ccfd1512d0 100644 --- a/drivers/mtd_sdcard/mtd_sdcard.c +++ b/drivers/mtd_sdcard/mtd_sdcard.c @@ -241,7 +241,7 @@ const mtd_desc_t mtd_sdcard_driver = { .params = &sdcard_spi_params[n] \ }; \ \ - mtd_dev_t CONCAT(*mtd, m) = (mtd_dev_t *)&mtd_sdcard_dev ## n + XFA_CONST(mtd_dev_xfa, m) mtd_dev_t CONCAT(*mtd, m) = (mtd_dev_t *)&mtd_sdcard_dev ## n #define MTD_SDCARD_DEV_FS(n, m, filesystem) \ VFS_AUTO_MOUNT(filesystem, VFS_MTD(mtd_sdcard_dev ## n), VFS_DEFAULT_SD(n), m) From 46040a43613bfaa1d69210399bfd09c9ed2cfaf4 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Tue, 11 Apr 2023 18:40:47 +0200 Subject: [PATCH 04/13] boards: use XFA with MTD pointers for defined MTDs --- boards/adafruit-grand-central-m4-express/board.c | 2 +- boards/adafruit-itsybitsy-m4/board.c | 2 +- boards/adafruit-pybadge/board.c | 2 +- boards/common/weact-f4x1cx/board.c | 2 +- boards/ikea-tradfri/board.c | 2 +- boards/iotlab-m3/mtd.c | 2 +- boards/mcb2388/board_init.c | 2 +- boards/msba2/board_init.c | 2 +- boards/mulle/board.c | 2 +- boards/native/board_init.c | 2 +- boards/nrf52840dk/mtd.c | 2 +- boards/nrf5340dk-app/board.c | 2 +- boards/nrf5340dk-app/include/board.h | 1 - boards/pinetime/board.c | 2 +- boards/qn9080dk/board.c | 2 +- boards/same54-xpro/board.c | 7 ++++--- boards/same54-xpro/include/board.h | 1 - boards/samr34-xpro/board.c | 2 +- boards/serpente/board.c | 2 +- 19 files changed, 20 insertions(+), 21 deletions(-) diff --git a/boards/adafruit-grand-central-m4-express/board.c b/boards/adafruit-grand-central-m4-express/board.c index 1461be647a..12c044c0db 100644 --- a/boards/adafruit-grand-central-m4-express/board.c +++ b/boards/adafruit-grand-central-m4-express/board.c @@ -59,7 +59,7 @@ static mtd_spi_nor_t samd51_nor_dev = { .params = &_samd51_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&samd51_nor_dev; +MTD_XFA_ADD(samd51_nor_dev, 0); #ifdef MODULE_VFS_DEFAULT VFS_AUTO_MOUNT(littlefs2, VFS_MTD(samd51_nor_dev), VFS_DEFAULT_NVM(0), 0); diff --git a/boards/adafruit-itsybitsy-m4/board.c b/boards/adafruit-itsybitsy-m4/board.c index 243c1f5dcd..3cfa05753a 100644 --- a/boards/adafruit-itsybitsy-m4/board.c +++ b/boards/adafruit-itsybitsy-m4/board.c @@ -54,7 +54,7 @@ static mtd_spi_nor_t samd51_nor_dev = { .params = &_samd51_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&samd51_nor_dev; +MTD_XFA_ADD(samd51_nor_dev, 0); #ifdef MODULE_VFS_DEFAULT VFS_AUTO_MOUNT(littlefs2, VFS_MTD(samd51_nor_dev), VFS_DEFAULT_NVM(0), 0); diff --git a/boards/adafruit-pybadge/board.c b/boards/adafruit-pybadge/board.c index adcb145d43..60268b48c1 100644 --- a/boards/adafruit-pybadge/board.c +++ b/boards/adafruit-pybadge/board.c @@ -54,7 +54,7 @@ static mtd_spi_nor_t samd51_nor_dev = { .params = &_samd51_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&samd51_nor_dev; +MTD_XFA_ADD(samd51_nor_dev, 0); #endif /* MODULE_MTD */ void board_init(void) diff --git a/boards/common/weact-f4x1cx/board.c b/boards/common/weact-f4x1cx/board.c index 806d808516..fa7a954d3e 100644 --- a/boards/common/weact-f4x1cx/board.c +++ b/boards/common/weact-f4x1cx/board.c @@ -51,7 +51,7 @@ static mtd_spi_nor_t weact_nor_dev = { .params = &_weact_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&weact_nor_dev; +MTD_XFA_ADD(weact_nor_dev, 0); #ifdef MODULE_VFS_DEFAULT #include "vfs_default.h" diff --git a/boards/ikea-tradfri/board.c b/boards/ikea-tradfri/board.c index 42786d08ca..e1f4197dd2 100644 --- a/boards/ikea-tradfri/board.c +++ b/boards/ikea-tradfri/board.c @@ -50,7 +50,7 @@ static mtd_spi_nor_t ikea_tradfri_nor_dev = { .params = &_ikea_tradfri_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&ikea_tradfri_nor_dev; +MTD_XFA_ADD(ikea_tradfri_nor_dev, 0); #endif /* MODULE_MTD */ void board_init(void) diff --git a/boards/iotlab-m3/mtd.c b/boards/iotlab-m3/mtd.c index 20e24df08b..958d6eb3d4 100644 --- a/boards/iotlab-m3/mtd.c +++ b/boards/iotlab-m3/mtd.c @@ -51,7 +51,7 @@ static mtd_spi_nor_t mtd_nor_dev = { .params = &_mtd_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&mtd_nor_dev; +MTD_XFA_ADD(mtd_nor_dev, 0); #ifdef MODULE_VFS_DEFAULT #include "vfs_default.h" diff --git a/boards/mcb2388/board_init.c b/boards/mcb2388/board_init.c index 351a732964..9d68c2fb66 100644 --- a/boards/mcb2388/board_init.c +++ b/boards/mcb2388/board_init.c @@ -28,7 +28,7 @@ #ifdef MODULE_MTD_MCI extern const mtd_desc_t mtd_mci_driver; static mtd_dev_t _mtd_mci = { .driver = &mtd_mci_driver }; -mtd_dev_t *mtd0 = &_mtd_mci; +MTD_XFA_ADD(_mtd_mci, 0); #endif #ifdef MODULE_VFS_DEFAULT diff --git a/boards/msba2/board_init.c b/boards/msba2/board_init.c index f7bc1e3f6e..4faeba6c1a 100644 --- a/boards/msba2/board_init.c +++ b/boards/msba2/board_init.c @@ -31,7 +31,7 @@ #ifdef MODULE_MTD_MCI extern const mtd_desc_t mtd_mci_driver; static mtd_dev_t _mtd_mci = { .driver = &mtd_mci_driver }; -mtd_dev_t *mtd0 = &_mtd_mci; +MTD_XFA_ADD(_mtd_mci, 0); #endif #ifdef MODULE_VFS_DEFAULT diff --git a/boards/mulle/board.c b/boards/mulle/board.c index c1c4d2876e..e7947c5d71 100644 --- a/boards/mulle/board.c +++ b/boards/mulle/board.c @@ -73,7 +73,7 @@ static mtd_spi_nor_t mulle_nor_dev = { .params = &mulle_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&mulle_nor_dev; +MTD_XFA_ADD(mulle_nor_dev, 0); static devfs_t mulle_nor_devfs = { .path = "/mtd0", diff --git a/boards/native/board_init.c b/boards/native/board_init.c index 613dbf5f49..0bb579c7aa 100644 --- a/boards/native/board_init.c +++ b/boards/native/board_init.c @@ -32,7 +32,7 @@ mtd_native_dev_t mtd0_dev = { .fname = MTD_NATIVE_FILENAME, }; -mtd_dev_t *mtd0 = &mtd0_dev.base; +MTD_XFA_ADD(mtd0_dev.base, 0); #endif #ifdef MODULE_VFS_DEFAULT diff --git a/boards/nrf52840dk/mtd.c b/boards/nrf52840dk/mtd.c index 2f26ce9cb3..a3a3221fbc 100644 --- a/boards/nrf52840dk/mtd.c +++ b/boards/nrf52840dk/mtd.c @@ -52,7 +52,7 @@ static mtd_spi_nor_t nrf52840dk_nor_dev = { .params = &_nrf52840dk_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&nrf52840dk_nor_dev; +MTD_XFA_ADD(nrf52840dk_nor_dev, 0); #ifdef MODULE_VFS_DEFAULT #include "vfs_default.h" diff --git a/boards/nrf5340dk-app/board.c b/boards/nrf5340dk-app/board.c index ff530addc6..582bc63837 100644 --- a/boards/nrf5340dk-app/board.c +++ b/boards/nrf5340dk-app/board.c @@ -50,7 +50,7 @@ static mtd_spi_nor_t nrf5340_nor_dev = { }, .params = &_nrf5340_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&nrf5340_nor_dev; +MTD_XFA_ADD(nrf5340_nor_dev, 0); #ifdef MODULE_VFS_DEFAULT VFS_AUTO_MOUNT(littlefs2, VFS_MTD(nrf5340_nor_dev), VFS_DEFAULT_NVM(0), 0); diff --git a/boards/nrf5340dk-app/include/board.h b/boards/nrf5340dk-app/include/board.h index 82917b7147..2d29603c1a 100644 --- a/boards/nrf5340dk-app/include/board.h +++ b/boards/nrf5340dk-app/include/board.h @@ -102,7 +102,6 @@ extern "C" { */ extern mtd_dev_t *mtd0; #define MTD_0 mtd0 -#define MTD_NUMOF 1 #define BOARD_QSPI_PIN_CS GPIO_PIN(0, 18) /**< SPI Flash Chip Select */ #define BOARD_QSPI_PIN_WP GPIO_PIN(0, 15) /**< SPI Flash Write Protect */ diff --git a/boards/pinetime/board.c b/boards/pinetime/board.c index 8039504d00..bdf8f6e7b4 100644 --- a/boards/pinetime/board.c +++ b/boards/pinetime/board.c @@ -55,7 +55,7 @@ static mtd_spi_nor_t pinetime_nor_dev = { .params = &_pinetime_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&pinetime_nor_dev; +MTD_XFA_ADD(pinetime_nor_dev, 0); #endif /* MODULE_MTD */ void board_init(void) diff --git a/boards/qn9080dk/board.c b/boards/qn9080dk/board.c index 7c53642757..b90cb6bdae 100644 --- a/boards/qn9080dk/board.c +++ b/boards/qn9080dk/board.c @@ -55,5 +55,5 @@ static mtd_spi_nor_t mtd_nor_dev = { .params = &_mtd_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&mtd_nor_dev; +MTD_XFA_ADD(mtd_nor_dev, 0); #endif /* MODULE_MTD */ diff --git a/boards/same54-xpro/board.c b/boards/same54-xpro/board.c index 1746b8c7d9..286f7b5155 100644 --- a/boards/same54-xpro/board.c +++ b/boards/same54-xpro/board.c @@ -51,7 +51,7 @@ static mtd_spi_nor_t same54_nor_dev = { }, .params = &_same54_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&same54_nor_dev; +MTD_XFA_ADD(same54_nor_dev, 0); #ifdef MODULE_VFS_DEFAULT VFS_AUTO_MOUNT(littlefs2, VFS_MTD(same54_nor_dev), VFS_DEFAULT_NVM(0), 0); @@ -69,7 +69,8 @@ static mtd_at24cxxx_t at24mac_dev = { .at24cxxx_eeprom = &at24cxxx_dev, .params = at24cxxx_params, }; -mtd_dev_t *mtd1 = (mtd_dev_t *)&at24mac_dev; +MTD_XFA_ADD(at24mac_dev, 1); + #endif /* MODULE_MTD_AT24CXXX */ #ifdef MODULE_SAM0_SDHC @@ -84,7 +85,7 @@ static mtd_sam0_sdhc_t sdhc_dev = { .wp = GPIO_PIN(PD, 21), }, }; -mtd_dev_t *mtd2 = (mtd_dev_t *)&sdhc_dev; +MTD_XFA_ADD(sdhc_dev, 2); #ifdef MODULE_VFS_DEFAULT /* default to FAT */ diff --git a/boards/same54-xpro/include/board.h b/boards/same54-xpro/include/board.h index 4669a736af..38cfb5b554 100644 --- a/boards/same54-xpro/include/board.h +++ b/boards/same54-xpro/include/board.h @@ -75,7 +75,6 @@ extern mtd_dev_t *mtd0, *mtd1, *mtd2; #define MTD_0 mtd0 #define MTD_1 mtd1 #define MTD_2 mtd2 -#define MTD_NUMOF 3 #define CONFIG_SDMMC_GENERIC_MTD_OFFSET 2 /**< mtd2 is used for SD Card */ /** @} */ diff --git a/boards/samr34-xpro/board.c b/boards/samr34-xpro/board.c index 2b7e3db44e..f9b01754e4 100644 --- a/boards/samr34-xpro/board.c +++ b/boards/samr34-xpro/board.c @@ -57,7 +57,7 @@ static mtd_spi_nor_t _nor_dev = { }, .params = &_mtd_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&_nor_dev; +MTD_XFA_ADD(_nor_dev, 0); #ifdef MODULE_VFS_DEFAULT #include "vfs_default.h" diff --git a/boards/serpente/board.c b/boards/serpente/board.c index e63deb5516..2dfb051b62 100644 --- a/boards/serpente/board.c +++ b/boards/serpente/board.c @@ -53,5 +53,5 @@ static mtd_spi_nor_t serpente_nor_dev = { .params = &_serpente_nor_params, }; -mtd_dev_t *mtd0 = (mtd_dev_t *)&serpente_nor_dev; +MTD_XFA_ADD(serpente_nor_dev, 0); #endif /* MODULE_MTD */ From d535277ebb0ae6ab6981a8c3e24a34cba19b8d0e Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Tue, 11 Apr 2023 18:42:01 +0200 Subject: [PATCH 05/13] cpu/esp_common: use XFA with MTD pointers for Flash MTD --- cpu/esp_common/periph/flash.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/cpu/esp_common/periph/flash.c b/cpu/esp_common/periph/flash.c index 16b5d2de90..129ea815ba 100644 --- a/cpu/esp_common/periph/flash.c +++ b/cpu/esp_common/periph/flash.c @@ -57,12 +57,12 @@ #define ESP_PART_ENTRY_SIZE 0x20 #define ESP_PART_ENTRY_MAGIC ESP_PARTITION_MAGIC -/* the external pointer to the system MTD device */ -mtd_dev_t* mtd0 = 0; - static mtd_dev_t _flash_dev; static mtd_desc_t _flash_driver; +/* the external pointer to the system MTD device */ +MTD_XFA_ADD(_flash_dev, 0); + #ifdef MODULE_VFS_DEFAULT #include "vfs_default.h" VFS_AUTO_MOUNT(littlefs2, { .dev = &_flash_dev }, VFS_DEFAULT_NVM(0), 0); @@ -205,8 +205,6 @@ void spi_flash_drive_init(void) _flash_dev.driver = &_flash_driver; _flash_dev.sector_count = _flash_size / _flashchip->sector_size; - mtd0 = &_flash_dev; - _flash_dev.pages_per_sector = _flashchip->sector_size / _flashchip->page_size; _flash_dev.page_size = _flashchip->page_size; /* Emulation for smaller / unaligned writes is present, but at reduced From a783a159af7a47c72ca67d55a8b84c0928f838cb Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Tue, 11 Apr 2023 18:43:27 +0200 Subject: [PATCH 06/13] sys/usbus/msc: use MTD pointers in XFA for LUN handling --- sys/include/usb/usbus/msc.h | 14 +------------- sys/usb/usbus/msc/msc.c | 16 ++++++++++------ 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/sys/include/usb/usbus/msc.h b/sys/include/usb/usbus/msc.h index b4048822a3..58e2e57d2c 100644 --- a/sys/include/usb/usbus/msc.h +++ b/sys/include/usb/usbus/msc.h @@ -50,17 +50,6 @@ extern "C" { */ #define USBUS_MSC_EP_OUT_REQUIRED_NUMOF 1 -/** - * @brief USBUS MSC Number of exported MTD device through USB - */ -#ifndef USBUS_MSC_EXPORTED_NUMOF -#ifdef MTD_NUMOF -#define USBUS_MSC_EXPORTED_NUMOF MTD_NUMOF -#else -#define USBUS_MSC_EXPORTED_NUMOF 0 -#endif -#endif /* USBUS_MSC_EXPORTED_NUMOF */ - /** * @brief USBUS MSC internal state machine enum */ @@ -102,8 +91,7 @@ typedef struct usbus_msc_device { uint16_t block_nb; /**< Number of block to transfer for READ and WRITE operations */ uint16_t block_offset; /**< Internal offset for endpoint size chunk transfer */ - usbus_msc_lun_t lun_dev[USBUS_MSC_EXPORTED_NUMOF]; /**< Array holding exported logical - unit descriptor */ + usbus_msc_lun_t *lun_dev; /**< Array holding exported logical unit descriptor */ } usbus_msc_device_t; /** diff --git a/sys/usb/usbus/msc/msc.c b/sys/usb/usbus/msc/msc.c index 05198ceae6..dec3581360 100644 --- a/sys/usb/usbus/msc/msc.c +++ b/sys/usb/usbus/msc/msc.c @@ -198,7 +198,7 @@ static unsigned _get_lun(usbus_msc_device_t *msc) /* Count only registered MTD devices as USB LUN, (using usbus_msc_add_lun) * not every MTD devices available on board */ - for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) { + for (unsigned i = 0; i < MTD_NUMOF; i++) { if (msc->lun_dev[i].mtd != NULL) { count++; } @@ -217,14 +217,14 @@ int usbus_msc_add_lun(usbus_t *usbus, mtd_dev_t *dev) } /* Check if MTD isn't already registered */ - for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) { + for (unsigned i = 0; i < MTD_NUMOF; i++) { if (dev == msc->lun_dev[i].mtd) { return -EBUSY; } } /* Store new MTD device in first slot available Also re alloc internal buffer if needed */ - for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) { + for (unsigned i = 0; i < MTD_NUMOF; i++) { if (msc->lun_dev[i].mtd == NULL) { int ret = mtd_init(dev); if (ret != 0) { @@ -283,7 +283,7 @@ int usbus_msc_remove_lun(usbus_t *usbus, mtd_dev_t *dev) usbus_msc_device_t *msc = _get_msc_handler(usbus); /* Identify the LUN to unexport */ - for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) { + for (unsigned i = 0; i < MTD_NUMOF; i++) { if (dev == msc->lun_dev[i].mtd) { /* Wait for any pending transaction to end */ @@ -329,7 +329,11 @@ static void _init(usbus_t *usbus, usbus_handler_t *handler) msc->block = 0; msc->state = WAITING; - for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) { + /* allocate the array holding exported logical unit descriptors */ + msc->lun_dev = malloc(sizeof(usbus_msc_lun_t) * MTD_NUMOF); + assert(msc->lun_dev); + + for (unsigned i = 0; i < MTD_NUMOF; i++) { msc->lun_dev[i].mtd = NULL; msc->lun_dev[i].block_size = 0; msc->lun_dev[i].block_nb = 0; @@ -369,7 +373,7 @@ static void _init(usbus_t *usbus, usbus_handler_t *handler) /* Auto-configure all MTD devices */ if (CONFIG_USBUS_MSC_AUTO_MTD) { - for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) { + for (unsigned i = 0; i < MTD_NUMOF; i++) { usbus_msc_add_lun(usbus, mtd_default_get_dev(i)); } } From 61c2259ac7dcac4ad434f9ea843bab2515f9bbff Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Wed, 12 Apr 2023 08:04:44 +0200 Subject: [PATCH 07/13] tests/mtd_raw: fix compilation XFA with MTD pointers --- tests/drivers/mtd_raw/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/drivers/mtd_raw/main.c b/tests/drivers/mtd_raw/main.c index feada0ec1f..8f10f9f11c 100644 --- a/tests/drivers/mtd_raw/main.c +++ b/tests/drivers/mtd_raw/main.c @@ -281,7 +281,7 @@ static int cmd_info(int argc, char **argv) if (argc < 2) { printf("mtd devices: %d\n", MTD_NUMOF); - for (int i = 0; i < MTD_NUMOF; ++i) { + for (unsigned i = 0; i < MTD_NUMOF; ++i) { printf(" -=[ MTD_%d ]=-\n", i); _print_info(mtd_default_get_dev(i)); } @@ -450,7 +450,7 @@ int main(void) puts("no MTD device present on the board."); } - for (int i = 0; i < MTD_NUMOF; ++i) { + for (unsigned i = 0; i < MTD_NUMOF; ++i) { printf("init MTD_%d… ", i); mtd_dev_t *dev = mtd_default_get_dev(i); From e70e6d7db26b56ed719e54034867dcdbe2dc720a Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Tue, 11 Apr 2023 18:44:21 +0200 Subject: [PATCH 08/13] drivers/mtd_emulated: use XFA with MTD pointers --- drivers/include/mtd_emulated.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/include/mtd_emulated.h b/drivers/include/mtd_emulated.h index 6e0195e3bc..7c881d9a3a 100644 --- a/drivers/include/mtd_emulated.h +++ b/drivers/include/mtd_emulated.h @@ -38,7 +38,10 @@ extern "C" { * creates the emulated MTD device `mtd_emulated_dev0` with 16 sectors, 4 pages * per sector and a page size of 64 bytes. The write size is always 1 byte. * - * @param n index of the emulated MTD (results into symbol `mtd_emulated_devn`) + * @note The emulated devices are added to the XFA of MTD device pointers + * @ref mtd_dev_xfa with priority 99 to place them at the end of the XFA. + * + * @param n index of the emulated MTD (results into symbol `mtd_emulated_dev`) * @param sc sectors of the emulated MTD * @param pps pages per sector of the emulated MTD * @param ps page size in bytes @@ -57,7 +60,9 @@ extern "C" { .size = sc * pps * ps, \ .memory = _mtd_emulated_memory ## n, \ .init_done = false, \ - } \ + }; \ + \ + XFA_CONST(mtd_dev_xfa, 99) mtd_dev_t CONCAT(*mtd_emulated, n) = (mtd_dev_t *)&mtd_emulated_dev ## n #if MODULE_VFS_AUTO_MOUNT || DOXYGEN /** @@ -70,7 +75,7 @@ extern "C" { * automatically mounts the emulated MTD `mtd_emulated_dev0` with FAT file * system under mount point `/mtde0` with unique index 2. * - * @param n index of the emulated MTD (symbol `mtd_emulated_devn`, mount point `/mtde0`) + * @param n index of the emulated MTD (symbol `mtd_emulated_dev`, mount point `/mtde0`) * @param m unique overall index of VFS mount point * @param fs filesystem type used */ From 9da0cc5bfd2f3c4832c604b371c96427fa89c15d Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Sun, 1 Oct 2023 18:01:09 +0200 Subject: [PATCH 09/13] drivers/mtd_sdmmc: use XFA with MTD pointers --- drivers/mtd_sdmmc/mtd_sdmmc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/mtd_sdmmc/mtd_sdmmc.c b/drivers/mtd_sdmmc/mtd_sdmmc.c index 624c95f250..ab0881af0d 100644 --- a/drivers/mtd_sdmmc/mtd_sdmmc.c +++ b/drivers/mtd_sdmmc/mtd_sdmmc.c @@ -235,7 +235,7 @@ const mtd_desc_t mtd_sdmmc_driver = { .sdmmc_idx = n, \ }; \ \ - mtd_dev_t CONCAT(*mtd, m) = (mtd_dev_t *)&mtd_sdmmc_dev ##n + XFA_CONST(mtd_dev_xfa, m) mtd_dev_t CONCAT(*mtd, m) = (mtd_dev_t *)&mtd_sdmmc_dev ## n #if IS_USED(MODULE_MTD_SDCARD_DEFAULT) /* we use /sd1 as default mount point for coexistence with mtd_sdcard */ From 31df2ba0aa80a19d84b03ae2e333ac512cc6e1d3 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Sat, 17 Sep 2022 22:28:01 +0200 Subject: [PATCH 10/13] Fletcher32: Add incremental API --- sys/checksum/fletcher32.c | 40 ++++++++++++++----- sys/include/checksum/fletcher32.h | 40 +++++++++++++++++++ .../tests-checksum-fletcher32.c | 15 +++++++ 3 files changed, 86 insertions(+), 9 deletions(-) diff --git a/sys/checksum/fletcher32.c b/sys/checksum/fletcher32.c index 5bbd306f3d..00babde6cc 100644 --- a/sys/checksum/fletcher32.c +++ b/sys/checksum/fletcher32.c @@ -21,21 +21,43 @@ #include "unaligned.h" #include "checksum/fletcher32.h" -uint32_t fletcher32(const uint16_t *data, size_t words) +static inline void _reduce(fletcher32_ctx_t *ctx) { - uint32_t sum1 = 0xffff, sum2 = 0xffff; + ctx->sum1 = (ctx->sum1 & 0xffff) + (ctx->sum1 >> 16); + ctx->sum2 = (ctx->sum2 & 0xffff) + (ctx->sum2 >> 16); +} +void fletcher32_init(fletcher32_ctx_t *ctx) +{ + ctx->sum1 = 0xffff; + ctx->sum2 = 0xffff; +} + +uint32_t fletcher32_finish(fletcher32_ctx_t *ctx) +{ + /* Second reduction step to reduce sums to 8 bits */ + _reduce(ctx); + return (ctx->sum2 << 16) | ctx->sum1; +} + +void fletcher32_update(fletcher32_ctx_t *ctx, const void *data, size_t words) +{ + const uint16_t *u16_data = (const uint16_t*)data; while (words) { unsigned tlen = words > 359 ? 359 : words; words -= tlen; do { - sum2 += sum1 += unaligned_get_u16(data++); + ctx->sum1 += unaligned_get_u16(u16_data++); + ctx->sum2 += ctx->sum1; } while (--tlen); - sum1 = (sum1 & 0xffff) + (sum1 >> 16); - sum2 = (sum2 & 0xffff) + (sum2 >> 16); + _reduce(ctx); } - /* Second reduction step to reduce sums to 16 bits */ - sum1 = (sum1 & 0xffff) + (sum1 >> 16); - sum2 = (sum2 & 0xffff) + (sum2 >> 16); - return (sum2 << 16) | sum1; +} + +uint32_t fletcher32(const uint16_t *data, size_t words) +{ + fletcher32_ctx_t ctx; + fletcher32_init(&ctx); + fletcher32_update(&ctx, data, words); + return fletcher32_finish(&ctx); } diff --git a/sys/include/checksum/fletcher32.h b/sys/include/checksum/fletcher32.h index 8f23851e97..01fc08dfcb 100644 --- a/sys/include/checksum/fletcher32.h +++ b/sys/include/checksum/fletcher32.h @@ -15,6 +15,7 @@ * * @file * @author Joakim Nohlgård + * @author Koen Zandberg */ #ifndef CHECKSUM_FLETCHER32_H @@ -27,6 +28,14 @@ extern "C" { #endif +/** + * @brief Fletcher's 32 bit checksum context struct + */ +typedef struct { + uint32_t sum1; /**< First sum of the checksum */ + uint32_t sum2; /**< Second sum of the checksum */ +} fletcher32_ctx_t; + /** * @brief Fletcher's 32 bit checksum * @@ -43,6 +52,37 @@ extern "C" { */ uint32_t fletcher32(const uint16_t *buf, size_t words); +/** + * @brief Initialize a fletcher32 context + * + * Multi-part version of @ref fletcher32. + * + * @param[in] ctx fletcher32 context to initialize + */ +void fletcher32_init(fletcher32_ctx_t *ctx); + +/** + * @brief Incrementally update the fletcher32 context with new data. Can be an arbitrary amount of + * times with new data to checksum. + * + * @note @p words is the number of 16 bit words in the buffer + * @note @p data should contain an integer number of 16 bit words + * + * @param[in] ctx fletcher32 context + * @param[in] data Data to add to the context + * @param[in] words Length of the data in 16 bit words + */ +void fletcher32_update(fletcher32_ctx_t *ctx, const void *data, size_t words); + +/** + * @brief Finalize the checksum operation and return the checksum + * + * @param[in] ctx fletcher32 context + * + * @return 32 bit sized hash in the interval [1..2^32] + */ +uint32_t fletcher32_finish(fletcher32_ctx_t *ctx); + #ifdef __cplusplus } #endif diff --git a/tests/unittests/tests-checksum/tests-checksum-fletcher32.c b/tests/unittests/tests-checksum/tests-checksum-fletcher32.c index 38dcba750b..cd3ed5cd3a 100644 --- a/tests/unittests/tests-checksum/tests-checksum-fletcher32.c +++ b/tests/unittests/tests-checksum/tests-checksum-fletcher32.c @@ -85,6 +85,20 @@ static void test_checksum_fletcher32_wrap_around(void) sizeof(wrap_around_data) - 1, expect)); } +static void test_checksum_fletcher32_wrap_around_piecewise(void) +{ + /* XXX: not verified with external implementation yet */ + uint32_t expect = 0x5bac8c3d; + fletcher32_ctx_t ctx; + fletcher32_init(&ctx); + size_t full_len = sizeof(wrap_around_data) - 1; + size_t initial_len = full_len / 2; + fletcher32_update(&ctx, wrap_around_data, initial_len / 2); + fletcher32_update(&ctx, (wrap_around_data + initial_len), (full_len - initial_len) / 2); + uint32_t result = fletcher32_finish(&ctx); + TEST_ASSERT_EQUAL_INT(result, expect); +} + Test *tests_checksum_fletcher32_tests(void) { EMB_UNIT_TESTFIXTURES(fixtures) { @@ -92,6 +106,7 @@ Test *tests_checksum_fletcher32_tests(void) new_TestFixture(test_checksum_fletcher32_0to1_undetected), new_TestFixture(test_checksum_fletcher32_atof), new_TestFixture(test_checksum_fletcher32_wrap_around), + new_TestFixture(test_checksum_fletcher32_wrap_around_piecewise), }; EMB_UNIT_TESTCALLER(checksum_fletcher32_tests, NULL, NULL, fixtures); From a00702ee65d6c44620d3842f1199b58072b85c4a Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Tue, 17 Oct 2023 10:14:30 +0200 Subject: [PATCH 11/13] Fletcher32: Rephrase note on 16 bit word operations --- sys/include/checksum/fletcher32.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sys/include/checksum/fletcher32.h b/sys/include/checksum/fletcher32.h index 01fc08dfcb..a53e02aa02 100644 --- a/sys/include/checksum/fletcher32.h +++ b/sys/include/checksum/fletcher32.h @@ -43,8 +43,7 @@ typedef struct { * http://en.wikipedia.org/w/index.php?title=Fletcher%27s_checksum&oldid=661273016#Optimizations * * @note the returned sum is never 0 - * @note pay attention to alignment issues since this operates on an input - * buffer containing 16 bit words, not bytes. + * @note pay attention to the @p words parameter buffer containing 16 bit words, not bytes. * * @param buf input buffer to hash * @param words length of buffer, in 16 bit words From 130420258cfb2ab2a0947b81166b90d1f3d54445 Mon Sep 17 00:00:00 2001 From: Lena Boeckmann Date: Thu, 19 Oct 2023 14:12:14 +0200 Subject: [PATCH 12/13] sys/psa_crypto: Fix conditions for public key max export size --- sys/include/psa_crypto/psa/crypto_sizes.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sys/include/psa_crypto/psa/crypto_sizes.h b/sys/include/psa_crypto/psa/crypto_sizes.h index 6fac20d386..ecf4f7fa60 100644 --- a/sys/include/psa_crypto/psa/crypto_sizes.h +++ b/sys/include/psa_crypto/psa/crypto_sizes.h @@ -965,7 +965,9 @@ extern "C" { * * See also @ref PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(@p key_type, @p key_bits). */ -#if IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P256R1) || IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P192R1) +#if (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P256R1) || \ + IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P192R1) || \ + IS_USED(MODULE_PSA_SECURE_ELEMENT_ATECCX08A_ECC_P256)) #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \ (PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_ECC_FAMILY_SECT_R1, PSA_MAX_PRIV_KEY_SIZE)) #else From b2e1c69b7922e2886e44345b2009dd36c986ad08 Mon Sep 17 00:00:00 2001 From: Lena Boeckmann Date: Thu, 19 Oct 2023 14:12:49 +0200 Subject: [PATCH 13/13] examples/psa_crypto: Update example to work with SEs --- examples/psa_crypto/example_ecdsa_p256.c | 11 ++++++++++- examples/psa_crypto/main.c | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/examples/psa_crypto/example_ecdsa_p256.c b/examples/psa_crypto/example_ecdsa_p256.c index 4f60a4916d..fe5fa8a170 100644 --- a/examples/psa_crypto/example_ecdsa_p256.c +++ b/examples/psa_crypto/example_ecdsa_p256.c @@ -81,10 +81,13 @@ psa_status_t example_ecdsa_p256(void) } #ifdef SECURE_ELEMENT + /* Currently there is no support for message signature and verification on secure elements */ psa_set_key_lifetime(&pubkey_attr, lifetime); + psa_set_key_usage_flags(&pubkey_attr, PSA_KEY_USAGE_VERIFY_HASH); +#else + psa_set_key_usage_flags(&pubkey_attr, PSA_KEY_USAGE_VERIFY_MESSAGE); #endif psa_set_key_algorithm(&pubkey_attr, ECC_ALG); - psa_set_key_usage_flags(&pubkey_attr, PSA_KEY_USAGE_VERIFY_MESSAGE); psa_set_key_bits(&pubkey_attr, PSA_BYTES_TO_BITS(pubkey_length)); psa_set_key_type(&pubkey_attr, PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1)); @@ -99,6 +102,12 @@ psa_status_t example_ecdsa_p256(void) return status; } +#ifdef SECURE_ELEMENT + /* Currently there is only support for hash signature and verification on secure elements, + so we can't verify the message, but only the hash */ + return psa_verify_hash(pubkey_id, ECC_ALG, hash, sizeof(hash), signature, sig_length); +#endif + /* verify on original message with internal hashing operation */ return psa_verify_message(pubkey_id, ECC_ALG, msg, sizeof(msg), signature, sig_length); } diff --git a/examples/psa_crypto/main.c b/examples/psa_crypto/main.c index b146d69335..a45e27220b 100644 --- a/examples/psa_crypto/main.c +++ b/examples/psa_crypto/main.c @@ -24,7 +24,10 @@ extern psa_status_t example_cipher_aes_128(void); extern psa_status_t example_hmac_sha256(void); extern psa_status_t example_ecdsa_p256(void); + +#ifndef SECURE_ELEMENT extern psa_status_t example_eddsa(void); +#endif #ifdef MULTIPLE_SE extern psa_status_t example_cipher_aes_128_sec_se(void); @@ -61,12 +64,14 @@ int main(void) printf("ECDSA failed: %s\n", psa_status_to_humanly_readable(status)); } +#ifndef SECURE_ELEMENT start = ztimer_now(ZTIMER_USEC); status = example_eddsa(); printf("EdDSA took %d us\n", (int)(ztimer_now(ZTIMER_USEC) - start)); if (status != PSA_SUCCESS) { printf("EdDSA failed: %s\n", psa_status_to_humanly_readable(status)); } +#endif #ifdef MULTIPLE_SE puts("Running Examples with secondary SE:");