From e8297b384238f797f847669884b85ee5f4063b5e Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 12 Nov 2026 11:07:41 -0800 Subject: [PATCH 1/9] Fix the disk updater build with ALLOW_DOWNGRADE=1 --- src/update_disk.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/update_disk.c b/src/update_disk.c index 50e37f9359..c635100970 100644 --- a/src/update_disk.c +++ b/src/update_disk.c @@ -991,6 +991,7 @@ void RAMFUNCTION wolfBoot_start(void) (unsigned int)(DISK_BLOCK_SIZE / 1024U)); #endif max_ver = (pB_ver_u > pA_ver_u) ? pB_ver_u : pA_ver_u; + (void)max_ver; /* unused with ALLOW_DOWNGRADE */ /* Choose partition with higher version */ selected = (pB_ver_u > pA_ver_u) ? 1 : 0; From a13a9b1fb85b10dc7faec68de39853c5984d95d3 Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 11 Nov 2026 15:08:46 -0800 Subject: [PATCH 2/9] Disable _FORTIFY_SOURCE for AArch64 so Linux GNU toolchains do not emit __memcpy_chk --- arch.mk | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch.mk b/arch.mk index 42c4fe20cb..de45f23dc3 100644 --- a/arch.mk +++ b/arch.mk @@ -71,6 +71,10 @@ ifeq ($(ARCH),AARCH64) CROSS_COMPILE?=aarch64-none-elf- endif CFLAGS+=-DARCH_AARCH64 -DFAST_MEMCPY + # Distribution aarch64-linux-gnu- compilers enable _FORTIFY_SOURCE by + # default, turning bounded memcpy() calls into glibc __memcpy_chk, which a + # bare-metal image cannot resolve (it links, then branches to address 0). + CFLAGS+=-U_FORTIFY_SOURCE ifeq ($(TARGET),aarch64_efi) # UEFI app: gnu-efi CRT0 is the entry; do_boot is in boot_aarch64_efi.o. # Skip the bare-metal reset (boot_aarch64_start.S) and EL2/GIC glue. From e32bdead11d26f253b2edd1ab3aec6270fde4b7c Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 12 Nov 2026 11:08:24 -0800 Subject: [PATCH 3/9] Build the AArch64 D-cache range routines for targets without the EL2 identity map --- hal/imx_usdhc_sdhci.c | 5 ++-- include/aarch64_arch.h | 8 ++++-- src/boot_aarch64_start.S | 53 ++++++++++++++++++++-------------------- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/hal/imx_usdhc_sdhci.c b/hal/imx_usdhc_sdhci.c index 77a2ba9278..131e01e0f5 100644 --- a/hal/imx_usdhc_sdhci.c +++ b/hal/imx_usdhc_sdhci.c @@ -393,7 +393,8 @@ uint32_t imx_usdhc_get_clock_khz(void) return usdhc_achieved_clk_khz; } -#if defined(WOLFBOOT_AARCH64_EL2_IDMAP) && !defined(SDHCI_SDMA_DISABLED) +#if (defined(WOLFBOOT_AARCH64_EL2_IDMAP) || \ + defined(WOLFBOOT_AARCH64_DCACHE_RANGE)) && !defined(SDHCI_SDMA_DISABLED) /* SDMA cache maintenance with the identity MMU on, by address range rather than * the set/way walk used at handoff, which would cost more per transfer than the * DMA saves. The range routines are in src/boot_aarch64_start.S. */ @@ -423,5 +424,5 @@ void sdhci_platform_dma_complete(void* buf, uint32_t sz, int is_write) if (!is_write && sz != 0) invalidate_dcache_range(start, start + sz); } -#endif /* WOLFBOOT_AARCH64_EL2_IDMAP && !SDHCI_SDMA_DISABLED */ +#endif /* (EL2_IDMAP || DCACHE_RANGE) && !SDHCI_SDMA_DISABLED */ #endif /* DISK_SDCARD || DISK_EMMC */ diff --git a/include/aarch64_arch.h b/include/aarch64_arch.h index afe9b3f41a..7fbbb82356 100644 --- a/include/aarch64_arch.h +++ b/include/aarch64_arch.h @@ -115,9 +115,13 @@ static inline void aarch64_dcache_maint(int clean) } #ifdef WOLFBOOT_AARCH64_EL2_IDMAP -/* src/boot_aarch64_start.S: identity MMU at EL2 and D-cache maintenance by - * address range (end exclusive). Stack-free. */ +/* src/boot_aarch64_start.S: identity MMU at EL2. Stack-free. */ void el2_idmap_enable(uint64_t dram_base, uint64_t dram_end); +#endif +#if defined(WOLFBOOT_AARCH64_EL2_IDMAP) || \ + defined(WOLFBOOT_AARCH64_DCACHE_RANGE) +/* src/boot_aarch64_start.S: D-cache maintenance by address range (end + * exclusive). Stack-free. */ void flush_dcache_range(uintptr_t start, uintptr_t end); /* Discards whole cache lines: a range that is not line-aligned also loses * the neighbouring data in its first and last lines. */ diff --git a/src/boot_aarch64_start.S b/src/boot_aarch64_start.S index aaa335baf9..9641a9acfb 100644 --- a/src/boot_aarch64_start.S +++ b/src/boot_aarch64_start.S @@ -1627,32 +1627,6 @@ el2_idmap_enable: ret .ltorg -/* - * void invalidate_dcache_range(start, end) - * - * invalidate data cache in the range, without writing it back - * (DMA reads; flush_dcache_range() is the clean & invalidate) - * - * x0: start address - * x1: end address - */ -.global invalidate_dcache_range -invalidate_dcache_range: - mrs x3, ctr_el0 - lsr x3, x3, #16 - and x3, x3, #0xf - mov x2, #4 - lsl x2, x2, x3 /* cache line size */ - - sub x3, x2, #1 - bic x0, x0, x3 -1: dc ivac, x0 - add x0, x0, x2 - cmp x0, x1 - b.lo 1b - dsb sy - ret - .pushsection .bss.el2_idmap_l1, "aw", %nobits .balign 4096 el2_idmap_l1: @@ -1723,6 +1697,33 @@ MMUTableL2: #endif /* !USE_BUILTIN_STARTUP */ +/* + * void invalidate_dcache_range(start, end) + * + * invalidate data cache in the range, without writing it back + * (DMA reads; flush_dcache_range() is the clean & invalidate) + * + * x0: start address + * x1: end address + */ +.global invalidate_dcache_range +invalidate_dcache_range: + mrs x3, ctr_el0 + lsr x3, x3, #16 + and x3, x3, #0xf + mov x2, #4 + lsl x2, x2, x3 /* cache line size */ + + sub x3, x2, #1 + bic x0, x0, x3 +1: dc ivac, x0 + add x0, x0, x2 + cmp x0, x1 + b.lo 1b + dsb sy + ret + + /* * void flush_dcache_range(start, end) * From 1d4c0a89e3bba73aff4e8ea9fb5838b0117d4aa0 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 12 Nov 2026 11:09:20 -0800 Subject: [PATCH 4/9] Publish the booted disk slot in the i.MX 8QuadMax device tree fixup --- hal/imx8qm.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hal/imx8qm.c b/hal/imx8qm.c index add20fb9bf..300da3fc10 100644 --- a/hal/imx8qm.c +++ b/hal/imx8qm.c @@ -1140,6 +1140,12 @@ int hal_dts_fixup(void* dts_addr, uint32_t capacity) wolfBoot_printf("FDT: bootargs fixup failed (%d)\n", ret); return ret; } +#ifdef WOLFBOOT_UPDATE_DISK + ret = wolfBoot_disk_dts_fixup(&ctx); + if (ret != 0) { + return ret; + } +#endif /* Opt-in "no-1-8-v" plus a clock cap for a path that cannot carry UHS-I, such as an SD * multiplexer. Off by default. The eMMC node is untouched; it needs 1.8V for HS200/400. */ From bbd2f4072b5bf58da358012db43609538eeae797 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 12 Nov 2026 13:59:40 -0800 Subject: [PATCH 5/9] Add fs_map to locate a file range on the media for the disk filesystem layer --- include/disk_fs.h | 8 ++++++ include/ext4.h | 12 +++++++++ include/fat32.h | 2 ++ src/disk_fs.c | 43 ++++++++++++++++++++++++++++++ src/ext4.c | 32 +++++++++++++++++++++++ src/fat32.c | 39 ++++++++++++++++++++++++++++ tools/unit-tests/unit-ext4.c | 44 +++++++++++++++++++++++++++++++ tools/unit-tests/unit-fat32.c | 49 +++++++++++++++++++++++++++++++++++ 8 files changed, 229 insertions(+) diff --git a/include/disk_fs.h b/include/disk_fs.h index 94419059c7..16c6fcc258 100644 --- a/include/disk_fs.h +++ b/include/disk_fs.h @@ -181,6 +181,14 @@ int fs_open(struct fs_volume *vol, struct fs_file *f, const char *path, * can switch between a file and a raw partition unchanged. */ int fs_read(struct fs_file *f, uint64_t off, uint64_t len, uint8_t *buf); +/* Partition-relative byte offset of a range of an open file, for a caller + * that rewrites bytes the file already owns (the boot state, with + * disk_part_write()). No filesystem structure is read for writing or + * changed. The range must lie inside the file and be backed by one + * contiguous run on the media: a hole or a range split across runs returns + * WOLFBOOT_FS_E_UNSUPPORTED. */ +int fs_map(struct fs_file *f, uint64_t off, uint32_t len, uint64_t *part_off); + /* Size in bytes of an open file. */ uint64_t fs_size(const struct fs_file *f); diff --git a/include/ext4.h b/include/ext4.h index 55660e1246..fdb63f6830 100644 --- a/include/ext4.h +++ b/include/ext4.h @@ -53,6 +53,7 @@ #define EXT4_B_FIRST_INO 0x1CU #define EXT4_B_INODE_SIZE 0x20U #define EXT4_B_FEATURE_INCOMPAT 0x28U +#define EXT4_B_FEATURE_RO_COMPAT 0x2CU /* Standalone superblock fields */ #define EXT4_SB_VOLUME_NAME 0x78U @@ -77,6 +78,14 @@ #define EXT4_INCOMPAT_EA_INODE 0x0400U #define EXT4_INCOMPAT_CSUM_SEED 0x2000U +/* Features a reader can ignore but a block rewrite cannot: a block may + * belong to other files too (shared_blocks), or be covered by a file's + * Merkle tree (verity). */ +#define EXT4_RO_COMPAT_SHARED_BLOCKS 0x4000U +#define EXT4_RO_COMPAT_VERITY 0x8000U +#define EXT4_RO_COMPAT_NO_REWRITE \ + (EXT4_RO_COMPAT_SHARED_BLOCKS | EXT4_RO_COMPAT_VERITY) + #define EXT4_INCOMPAT_SUPPORTED ( \ EXT4_INCOMPAT_FILETYPE /* dirent type byte; optional, handled */ \ | EXT4_INCOMPAT_EXTENTS /* required outright, see ext4_mount */ \ @@ -135,6 +144,7 @@ struct ext4_volume { uint32_t first_data_block; uint32_t groups; uint32_t incompat; /* validated feature word (FILETYPE is read) */ + uint32_t ro_compat; /* only consulted before a block is rewritten */ uint16_t inode_sz; uint16_t desc_sz; /* 32, or s_desc_size when INCOMPAT_64BIT */ }; @@ -147,6 +157,8 @@ int ext4_mount(struct fs_volume *vol); int ext4_open(struct fs_volume *vol, struct fs_file *f, const char *path, uint64_t max_size); int ext4_read(struct fs_file *f, uint64_t off, uint64_t len, uint8_t *buf); +int ext4_map(struct fs_file *f, uint64_t off, uint32_t len, + uint64_t *part_off); int ext4_label_eq(struct fs_volume *vol, const char *label); #endif /* WOLFBOOT_EXT4_H */ diff --git a/include/fat32.h b/include/fat32.h index 56baeb2349..cb41abdede 100644 --- a/include/fat32.h +++ b/include/fat32.h @@ -121,6 +121,8 @@ int fat32_mount(struct fs_volume *vol); int fat32_open(struct fs_volume *vol, struct fs_file *f, const char *path, uint64_t max_size); int fat32_read(struct fs_file *f, uint64_t off, uint64_t len, uint8_t *buf); +int fat32_map(struct fs_file *f, uint64_t off, uint32_t len, + uint64_t *part_off); int fat32_label_eq(struct fs_volume *vol, const char *label); #endif /* WOLFBOOT_FAT32_H */ diff --git a/src/disk_fs.c b/src/disk_fs.c index ee1360d907..cd04e8d8e8 100644 --- a/src/disk_fs.c +++ b/src/disk_fs.c @@ -415,6 +415,49 @@ int fs_read(struct fs_file *f, uint64_t off, uint64_t len, uint8_t *buf) return ret; } +int fs_map(struct fs_file *f, uint64_t off, uint32_t len, uint64_t *part_off) +{ + uint64_t poff = 0; + int ret; + + if ((f == NULL) || (f->vol == NULL) || (part_off == NULL) || + (len == 0U)) { + return WOLFBOOT_FS_E_PARAM; + } + if ((off > f->size) || ((uint64_t)len > (f->size - off))) { + return WOLFBOOT_FS_E_RANGE; + } + + switch (f->vol->type) { + case FS_TYPE_RAW: + poff = off; + ret = WOLFBOOT_FS_OK; + break; +#ifdef WOLFBOOT_FAT32 + case FS_TYPE_FAT32: + ret = fat32_map(f, off, len, &poff); + break; +#endif +#ifdef WOLFBOOT_EXT4 + case FS_TYPE_EXT4: + ret = ext4_map(f, off, len, &poff); + break; +#endif + default: + ret = WOLFBOOT_FS_E_PARAM; + break; + } + if (ret != WOLFBOOT_FS_OK) { + return ret; + } + if ((poff > f->vol->part_sz) || + ((uint64_t)len > (f->vol->part_sz - poff))) { + return WOLFBOOT_FS_E_RANGE; + } + *part_off = poff; + return WOLFBOOT_FS_OK; +} + uint64_t fs_size(const struct fs_file *f) { if (f == NULL) { diff --git a/src/ext4.c b/src/ext4.c index 23db3a965d..176b30d047 100644 --- a/src/ext4.c +++ b/src/ext4.c @@ -249,6 +249,7 @@ int ext4_mount(struct fs_volume *vol) ev->first_data_block = first_data_block; ev->groups = (uint32_t)groups; ev->incompat = incompat; + ev->ro_compat = fs_le32(sb + EXT4_B_FEATURE_RO_COMPAT); ev->inode_sz = inode_sz; ev->desc_sz = desc_sz; ev->gdt_off = gdt_off; @@ -896,6 +897,37 @@ int ext4_read(struct fs_file *f, uint64_t off, uint64_t len, uint8_t *buf) return (int)done; } +int ext4_map(struct fs_file *f, uint64_t off, uint32_t len, + uint64_t *part_off) +{ + struct fs_volume *vol = f->vol; + struct ext4_volume *ev = &vol->u.ext; + uint64_t lblk, pblk; + uint32_t in_blk, run; + int is_hole; + int ret; + + if ((ev->ro_compat & EXT4_RO_COMPAT_NO_REWRITE) != 0U) { + return WOLFBOOT_FS_E_UNSUPPORTED; + } + + lblk = off / (uint64_t)ev->block_sz; + in_blk = (uint32_t)(off % (uint64_t)ev->block_sz); + + ret = ext4_map_run(vol, f->iblock, lblk, &pblk, &run, &is_hole); + if (ret != WOLFBOOT_FS_OK) { + return ret; + } + /* A hole has no block to rewrite, and a range that leaves this extent + * is not one run on the media. */ + if ((is_hole != 0) || ((((uint64_t)run * (uint64_t)ev->block_sz) - + in_blk) < (uint64_t)len)) { + return WOLFBOOT_FS_E_UNSUPPORTED; + } + *part_off = (pblk * (uint64_t)ev->block_sz) + in_blk; + return WOLFBOOT_FS_OK; +} + int ext4_label_eq(struct fs_volume *vol, const char *label) { char vl[EXT4_SB_VOLUME_NAME_LEN + 1]; diff --git a/src/fat32.c b/src/fat32.c index 69d561b6fa..de12ab681a 100644 --- a/src/fat32.c +++ b/src/fat32.c @@ -775,6 +775,45 @@ int fat32_read(struct fs_file *f, uint64_t off, uint64_t len, uint8_t *buf) return (int)done; } +int fat32_map(struct fs_file *f, uint64_t off, uint32_t len, + uint64_t *part_off) +{ + struct fs_volume *vol = f->vol; + struct fat32_volume *fv = &vol->u.fat; + uint64_t covered; + uint32_t in_clus, clus, next; + int ret; + + ret = fat32_seek(f, off); + if (ret != WOLFBOOT_FS_OK) { + return ret; + } + clus = f->cur_clus; + in_clus = (uint32_t)(off % (uint64_t)fv->clus_sz); + + /* A range that runs past this cluster is one run on the media only if + * every following link is the physically next cluster. */ + covered = (uint64_t)fv->clus_sz - in_clus; + while (covered < (uint64_t)len) { + ret = fat32_next_clus(vol, clus, &next); + if (ret < 0) { + return ret; + } + if (ret == 1) { + return WOLFBOOT_FS_E_CORRUPT; + } + if (next != (clus + 1U)) { + return WOLFBOOT_FS_E_UNSUPPORTED; + } + clus = next; + covered += (uint64_t)fv->clus_sz; + } + + *part_off = fv->data_off + + (((uint64_t)f->cur_clus - 2U) * (uint64_t)fv->clus_sz) + in_clus; + return WOLFBOOT_FS_OK; +} + int fat32_label_eq(struct fs_volume *vol, const char *label) { char vl[FAT_BS_VOL_LAB_LEN + 1]; diff --git a/tools/unit-tests/unit-ext4.c b/tools/unit-tests/unit-ext4.c index c92ce33667..55875ffe18 100644 --- a/tools/unit-tests/unit-ext4.c +++ b/tools/unit-tests/unit-ext4.c @@ -454,6 +454,48 @@ START_TEST(test_ext4_extents_are_coalesced) } END_TEST +/* fs_map() must name the bytes fs_read() returns: the caller rewrites them + * in place on the media. The image is partition 0, so a partition offset is + * an offset into the image file. */ +static void check_map(const char *img) +{ + struct fs_volume vol; + struct fs_file f; + uint64_t plen, poff, off; + uint8_t *payload = load_file(PAYLOAD_PATH, &plen); + uint8_t got[8]; + unsigned int i; + + mount_img(&vol, img); + ck_assert_int_eq(fs_open(&vol, &f, IMG_FILE, plen), WOLFBOOT_FS_OK); + for (i = 0; i < 3; i++) { + off = (i == 0) ? 0 : ((i == 1) ? 70000 : (plen - sizeof(got))); + poff = 0; + ck_assert_int_eq(fs_map(&f, off, sizeof(got), &poff), + WOLFBOOT_FS_OK); + ck_assert_int_eq(fseeko(img_fp, (off_t)poff, SEEK_SET), 0); + ck_assert_uint_eq(fread(got, 1, sizeof(got), img_fp), sizeof(got)); + ck_assert_int_eq(memcmp(got, payload + off, sizeof(got)), 0); + } + /* The payload's first zero run is stored as a hole: no block to name. */ + ck_assert_int_eq(fs_map(&f, 16384, sizeof(got), &poff), + WOLFBOOT_FS_E_UNSUPPORTED); + ck_assert_int_eq(fs_map(&f, plen - 4U, 8, &poff), WOLFBOOT_FS_E_RANGE); + free(payload); +} + +START_TEST(test_ext4_map_minimal) +{ + check_map(MIN_IMG); +} +END_TEST + +START_TEST(test_ext4_map_stock) +{ + check_map(STOCK_IMG); +} +END_TEST + static Suite *wolfboot_suite(void) { Suite *s = suite_create("wolfboot-ext4"); @@ -482,6 +524,8 @@ static Suite *wolfboot_suite(void) tcase_add_test(tc_read, test_ext4_read_backwards_and_eof); tcase_add_test(tc_read, test_ext4_read_nested); tcase_add_test(tc_read, test_ext4_extents_are_coalesced); + tcase_add_test(tc_read, test_ext4_map_minimal); + tcase_add_test(tc_read, test_ext4_map_stock); suite_add_tcase(s, tc_read); return s; diff --git a/tools/unit-tests/unit-fat32.c b/tools/unit-tests/unit-fat32.c index 4ad2e00770..0b8d113652 100644 --- a/tools/unit-tests/unit-fat32.c +++ b/tools/unit-tests/unit-fat32.c @@ -490,6 +490,53 @@ START_TEST(test_fat32_contiguous_runs_are_coalesced) } END_TEST +/* fs_map() must name the bytes fs_read() returns: the caller rewrites them + * in place on the media. The image is partition 0, so a partition offset is + * an offset into the image file. */ +START_TEST(test_fat32_map_matches_media) +{ + static const uint64_t offs[] = { 0, 1, 508, 511, 512, 70000 }; + struct fs_volume vol; + struct fs_file f; + uint64_t plen, poff; + uint8_t *payload = load_file(PAYLOAD_PATH, &plen); + uint8_t got[8]; + unsigned int i; + + mount_ok(&vol); + ck_assert_int_eq(fs_open(&vol, &f, LFN_NAME, plen), WOLFBOOT_FS_OK); + for (i = 0; i <= sizeof(offs) / sizeof(offs[0]); i++) { + uint64_t off = (i < sizeof(offs) / sizeof(offs[0])) ? + offs[i] : (plen - sizeof(got)); + + poff = 0; + ck_assert_int_eq(fs_map(&f, off, sizeof(got), &poff), + WOLFBOOT_FS_OK); + ck_assert_int_eq(fseeko(img_fp, (off_t)poff, SEEK_SET), 0); + ck_assert_uint_eq(fread(got, 1, sizeof(got), img_fp), sizeof(got)); + ck_assert_int_eq(memcmp(got, payload + off, sizeof(got)), 0); + } + free(payload); +} +END_TEST + +START_TEST(test_fat32_map_rejects_out_of_file) +{ + struct fs_volume vol; + struct fs_file f; + uint64_t plen, poff = 0; + uint8_t *payload = load_file(PAYLOAD_PATH, &plen); + + mount_ok(&vol); + ck_assert_int_eq(fs_open(&vol, &f, LFN_NAME, plen), WOLFBOOT_FS_OK); + ck_assert_int_eq(fs_map(&f, plen - 4U, 8, &poff), WOLFBOOT_FS_E_RANGE); + ck_assert_int_eq(fs_map(&f, plen, 8, &poff), WOLFBOOT_FS_E_RANGE); + ck_assert_int_eq(fs_map(&f, 0, 0, &poff), WOLFBOOT_FS_E_PARAM); + ck_assert_int_eq(fs_map(&f, 0, 8, NULL), WOLFBOOT_FS_E_PARAM); + free(payload); +} +END_TEST + static Suite *wolfboot_suite(void) { Suite *s = suite_create("wolfboot-fat32"); @@ -520,6 +567,8 @@ static Suite *wolfboot_suite(void) tcase_add_test(tc_read, test_fat32_read_eof); tcase_add_test(tc_read, test_fat32_read_small_files); tcase_add_test(tc_read, test_fat32_contiguous_runs_are_coalesced); + tcase_add_test(tc_read, test_fat32_map_matches_media); + tcase_add_test(tc_read, test_fat32_map_rejects_out_of_file); suite_add_tcase(s, tc_read); return s; From b4fd2d9b99702f5fca105670d017ab873c04120b Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 12 Nov 2026 14:02:33 -0800 Subject: [PATCH 6/9] Extend disk boot confirmation to FAT32 and ext4 file slots --- .gitignore | 1 + docs/compile.md | 16 +- hal/library_fs.c | 3 +- src/update_disk.c | 107 ++++++++++-- tools/unit-tests/Makefile | 14 +- tools/unit-tests/unit-update-disk-fs.c | 226 ++++++++++++++++++++++++- 6 files changed, 348 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 00ef06d1ca..f4bc059885 100644 --- a/.gitignore +++ b/.gitignore @@ -156,6 +156,7 @@ tools/unit-tests/unit-fs-malicious tools/unit-tests/unit-ext4 tools/unit-tests/unit-update-disk-fs tools/unit-tests/unit-update-disk-fs-enc +tools/unit-tests/unit-update-disk-fs-confirm tools/unit-tests/ext4-min.img tools/unit-tests/ext4-stock.img tools/unit-tests/ext4-payload.bin diff --git a/docs/compile.md b/docs/compile.md index 965d1a0025..7cab2be99a 100644 --- a/docs/compile.md +++ b/docs/compile.md @@ -581,9 +581,9 @@ For more details about this optional feature, please refer to the [Encrypted ext The disk boot path (`src/update_disk.c`) is stateless by default: it selects the highest-versioned slot, verifies it, and falls over to the other slot when *verification* fails. It writes nothing back, so an image that verifies and then fails to **boot** is retried indefinitely. -`DISK_BOOT_CONFIRM=1` closes that gap using the **same partition trailer** the sector-swap flow in `src/update_flash.c` already keeps, placed in the tail of the raw boot partition: +`DISK_BOOT_CONFIRM=1` closes that gap using the **same partition trailer** the sector-swap flow in `src/update_flash.c` already keeps, placed in the tail of the slot. For a raw boot partition that is the tail of the partition; for a `DISK_FS` slot it is the tail of the image file: -| Offset from the end of the partition | Size | Meaning | +| Offset from the end of the partition or file | Size | Meaning | | --- | --- | --- | | -4 | 4 | Magic, the ASCII `BOOT` (`WOLFBOOT_MAGIC_TRAIL`) | | -5 | 1 | Partition state | @@ -615,6 +615,13 @@ lib-fs --dev /dev/mmcblk1p1 success # once the system is known good lib-fs --dev /dev/mmcblk1p1 status # read the current state ``` +For a file slot, mount the filesystem and give `--dev` the image file itself. The tool finds the trailer from the size of what it opens, so the same three commands apply: + +``` +lib-fs --dev /boot/fitImage_b.bin stage +lib-fs --dev /boot/fitImage_b.bin success +``` + `update-trigger` is refused with `--dev`: it marks the separate UPDATE partition at a compile-time offset, which against a raw slot device is just somewhere in the middle of the slot. `stage` is the disk analogue. Order the `success` call after whatever the system treats as proof of a healthy boot. Three placements are common, and the choice belongs to the integrator: @@ -623,7 +630,7 @@ Order the `success` call after whatever the system treats as proof of a healthy - A unit ordered after a specific mount or service (`After=` and `Requires=` on `var-log.mount`, a database, a network target): the boot is only healthy when that resource came up, so a kernel that boots but cannot bring it up is rolled back. - The application itself, once it has reported in to whatever it serves: nothing is confirmed on a system whose purpose is not met. Ship the tool, not the unit. -Whatever confirms needs to know which slot it is running from, and the rootfs cannot tell it: both slots boot the same rootfs. wolfBoot writes the GPT index of the slot it verified into the device tree it hands to the OS, as `/chosen/wolfboot,boot-part` (32-bit big-endian, the same 0-based index as `BOOT_PART_A` / `BOOT_PART_B`), so on Linux the slot is `/proc/device-tree/chosen/wolfboot,boot-part` and the partition device is that index plus one on the boot disk. The same value is available to a wolfBoot hook as `wolfBoot_disk_boot_part()`. A device tree that cannot carry the property halts the boot: handing the OS a tree that names no slot would leave that slot unconfirmed, and one that still carries a value its author wrote would aim the confirmation at the wrong slot. A confirm step that checks the slot's `status` first and only calls `success` when it reads `testing` keeps the steady state write-free. +Whatever confirms needs to know which slot it is running from, and the rootfs cannot tell it: both slots boot the same rootfs. wolfBoot writes the GPT index of the slot it verified into the device tree it hands to the OS, as `/chosen/wolfboot,boot-part` (32-bit big-endian, the same 0-based index as `BOOT_PART_A` / `BOOT_PART_B`), so on Linux the slot is `/proc/device-tree/chosen/wolfboot,boot-part` and the partition device is that index plus one on the boot disk. When the slot is a file, `/chosen/wolfboot,boot-file` carries its path within that partition as well, because two file slots can share one partition. For a raw slot the property is not added, and one the device tree already carried is set to an empty string. The same value is available to a wolfBoot hook as `wolfBoot_disk_boot_part()`. A device tree that cannot carry the property halts the boot: handing the OS a tree that names no slot would leave that slot unconfirmed, and one that still carries a value its author wrote would aim the confirmation at the wrong slot. A confirm step that checks the slot's `status` first and only calls `success` when it reads `testing` keeps the steady state write-free. The tool needs no configuration to match the loader's layout and no rebuild per slot. With `--dev` it locates the trailer from the size of the device it was handed, which is how wolfBoot locates it too, and it writes the pinned state values rather than the `IMG_STATE_*` of its own build. `include/disk_trailer.h` holds the offset, the magic and the four state values, and both the loader and the tool include it, so there is one definition to disagree with rather than two. @@ -631,7 +638,8 @@ The tool needs no configuration to match the loader's layout and no rebuild per - The trailer must not overlap the image, so the partition has to be larger than the signed image by at least the 8 bytes of the trailer. wolfBoot refuses the write and reports it rather than corrupting the image, and still boots: the consequence of no trailer is no confirmation, not a dead system. - A partition smaller than one 512-byte sector cannot carry a trailer and is likewise never armed. -- **Raw partitions only.** A `DISK_FS` slot is a file inside a filesystem and has no partition tail to claim. +- **File slots (`DISK_FS`) keep the trailer in the image file.** The file must be longer than the signed image by at least the 8 bytes of the trailer and at least 512 bytes in all, and still within the size wolfBoot accepts for an image. wolfBoot finds the file's last bytes through the same read-only lookup it loads the image with and rewrites those 8 bytes in place with a block write: nothing is allocated, and no directory entry, allocation table, inode or checksum changes. A file with no padding is never armed, like a partition too small for a trailer. +- The padding must be stored data. On ext4 a tail left as a hole (`truncate -s`) or as an unwritten extent (`fallocate`) has no block to rewrite, and a trailer split across two extents or two non-adjacent FAT clusters is refused; append real bytes instead (`head -c 4096 /dev/zero >> image`). An ext4 volume with an unreplayed journal is not mounted at all. On an ext4 volume with the `shared_blocks` or `verity` feature a file's block may belong to other files too, or be covered by a Merkle tree, so such a slot is never armed. - A slot left in `testing` is refused on every path into it, including the failover after another slot fails verification, so it stays out of the boot even in an `ALLOW_DOWNGRADE` build where the version guard is compiled out. ### Golden slot diff --git a/hal/library_fs.c b/hal/library_fs.c index 813809e4f1..2259be72cf 100644 --- a/hal/library_fs.c +++ b/hal/library_fs.c @@ -347,7 +347,8 @@ static void print_usage(const char* prog_name) wolfBoot_printf("\nOptions:\n"); wolfBoot_printf(" --dev - Operate on this backing store instead of the\n"); wolfBoot_printf(" image built in. With a disk boot slot's\n"); - wolfBoot_printf(" partition device, \"status\", \"stage\" and\n"); + wolfBoot_printf(" partition device, or its image file on a\n"); + wolfBoot_printf(" mounted filesystem, \"status\", \"stage\" and\n"); wolfBoot_printf(" \"success\" act on that slot's own tail.\n"); wolfBoot_printf("\nPartitions:\n"); wolfBoot_printf(" BOOT - Currently running firmware partition\n"); diff --git a/src/update_disk.c b/src/update_disk.c index c635100970..9bc0908c1c 100644 --- a/src/update_disk.c +++ b/src/update_disk.c @@ -405,6 +405,9 @@ extern uint8_t _end_wb[]; struct boot_slot { int part; int ready; +#ifdef DISK_BOOT_CONFIRM + uint64_t img_end; /* first byte past the image, 0 while not known */ +#endif #ifdef WOLFBOOT_DISK_FS const char *file; struct fs_volume vol; @@ -430,38 +433,75 @@ static uint8_t slot_state[DISK_SLOTS]; /* Slots disk_boot_state_reap() took out of the election. */ static uint8_t slot_reaped[DISK_SLOTS]; -/* Byte offset of the trailer within the slot's partition, or -1 when the slot - * cannot carry one. */ -static int slot_trailer_off(struct boot_slot *s, uint64_t *off) +/* Where a slot keeps its trailer: the tail of the raw partition, or of the + * image file on a filesystem slot. rel is the offset within the slot, to + * compare against the image end; off is the partition offset to read and + * write. -1 when the slot cannot carry one. */ +static int slot_trailer_off(struct boot_slot *s, uint64_t *rel, uint64_t *off) { uint64_t sz = 0; - if ((s == NULL) || (off == NULL) || (s->ready == 0)) { + if ((s == NULL) || (rel == NULL) || (off == NULL) || (s->ready == 0)) { return -1; } #ifdef WOLFBOOT_DISK_FS - /* A file-backed slot has no partition tail to claim: the filesystem owns - * it. Boot confirmation is raw-partition only. */ + /* The filesystem owns the partition tail, so a file-backed slot keeps + * the trailer in the last bytes of its own file. Those bytes are located + * through the read path and rewritten in place: nothing is allocated + * and no filesystem structure changes. A file whose tail is a hole or + * is split across runs cannot carry one. */ if (s->vol.type != FS_TYPE_RAW) { - return -1; + if (disk_trailer_offset(fs_size(&s->f), rel) != 0) { + return -1; + } + if (fs_map(&s->f, *rel, DISK_TRAILER_SZ, off) != WOLFBOOT_FS_OK) { + return -1; + } + return 0; } #endif if (disk_part_size(BOOT_DISK, s->part, &sz) != 0) { return -1; } - return disk_trailer_offset(sz, off); + if (disk_trailer_offset(sz, rel) != 0) { + return -1; + } + *off = *rel; + return 0; +} + +/* Record where the slot's image ends, from its plaintext header. */ +static void slot_note_image(struct boot_slot *s, const uint8_t *hdr) +{ + uint32_t fw_size; + + /* By bytes: WOLFBOOT_MAGIC as a word depends on the host byte order, + * the header on the media does not. */ + if ((hdr[0] != 'W') || (hdr[1] != 'O') || (hdr[2] != 'L') || + (hdr[3] != 'F')) { + return; + } + fw_size = (uint32_t)hdr[4] | ((uint32_t)hdr[5] << 8) | + ((uint32_t)hdr[6] << 16) | ((uint32_t)hdr[7] << 24); + s->img_end = (uint64_t)IMAGE_HEADER_SIZE + (uint64_t)fw_size; } /* Current state of a slot. */ static int slot_state_read(struct boot_slot *s, uint8_t *state) { + uint64_t rel = 0; uint64_t off = 0; if (state == NULL) { return -1; } *state = DISK_STATE_NEW; - if (slot_trailer_off(s, &off) != 0) { + if (slot_trailer_off(s, &rel, &off) != 0) { + return -1; + } + /* A trailer position inside the image holds image data, whatever it + * decodes to. */ + if (rel < s->img_end) { return -1; } if (disk_part_read(BOOT_DISK, s->part, off, DISK_TRAILER_SZ, @@ -477,12 +517,13 @@ static int slot_state_read(struct boot_slot *s, uint8_t *state) static int slot_state_write(struct boot_slot *s, uint8_t state, uint64_t img_end) { + uint64_t rel = 0; uint64_t off = 0; - if (slot_trailer_off(s, &off) != 0) { + if (slot_trailer_off(s, &rel, &off) != 0) { return -1; } - if (off < img_end) { + if (rel < img_end) { wolfBoot_printf("Boot state would overlap the image on p%d\r\n", s->part); return -1; @@ -492,6 +533,9 @@ static int slot_state_write(struct boot_slot *s, uint8_t state, disk_trailer) != (int)DISK_TRAILER_SZ) { return -1; } +#ifdef WOLFBOOT_DISK_FS + fs_cache_invalidate(); +#endif return 0; } @@ -532,6 +576,8 @@ static void disk_boot_state_reap(uint32_t *pA_ver, uint32_t *pB_ver) } } } +#else +#define slot_note_image(s, hdr) do { } while (0) #endif /* DISK_BOOT_CONFIRM */ #ifdef DISK_FAILOVER_ZEROIZE @@ -739,6 +785,11 @@ int wolfBoot_disk_boot_part(void) return disk_boot_part; } +#if defined(WOLFBOOT_DISK_FS) && defined(DISK_BOOT_CONFIRM) +/* Image file of that slot when it is on a filesystem, else NULL. */ +static const char *disk_boot_file = NULL; +#endif + #if defined(MMU) || defined(WOLFBOOT_FDT) /* Tell the OS which slot it is booting from, as /chosen/wolfboot,boot-part, * so whatever confirms the boot (lib-fs success) can address that slot and @@ -749,6 +800,9 @@ int wolfBoot_disk_boot_part(void) int wolfBoot_disk_dts_fixup(fdt_ctx* ctx) { int ret; +#if defined(WOLFBOOT_DISK_FS) && defined(DISK_BOOT_CONFIRM) + int len = 0; +#endif if (disk_boot_part < 0) { return 0; @@ -762,6 +816,28 @@ int wolfBoot_disk_dts_fixup(fdt_ctx* ctx) wolfBoot_printf("FDT: Failed to set wolfboot,boot-part (%d)\r\n", ret); wolfBoot_panic(); } +#if defined(WOLFBOOT_DISK_FS) && defined(DISK_BOOT_CONFIRM) + /* Two file slots can share a partition, so the index alone does not + * name the slot: publish the file that carries its boot state too. A + * raw slot has no file, and a value the tree already carried is blanked + * so it cannot point the confirmation at another slot. */ + ret = fdt_subnode_offset(ctx, 0, "chosen"); + if ((ret >= 0) && (disk_boot_file == NULL) && + (fdt_getprop(ctx, ret, "wolfboot,boot-file", &len) == NULL)) { + ret = 0; + } + else { + if (ret >= 0) { + ret = fdt_fixup_str(ctx, ret, "chosen", "wolfboot,boot-file", + (disk_boot_file != NULL) ? disk_boot_file : ""); + } + if (ret != 0) { + wolfBoot_printf("FDT: Failed to set wolfboot,boot-file (%d)\r\n", + ret); + wolfBoot_panic(); + } + } +#endif return ret; } #endif @@ -932,9 +1008,11 @@ void RAMFUNCTION wolfBoot_start(void) /* Use local version parser to avoid double-decryption issue * when EXT_ENCRYPTED && MMU is also defined */ pA_ver = get_decrypted_blob_version(dec_hdr); + slot_note_image(&boot_slots[0], dec_hdr); } #else pA_ver = wolfBoot_get_blob_version((uint8_t*)p_hdr); + slot_note_image(&boot_slots[0], p_hdr); #endif } @@ -947,9 +1025,11 @@ void RAMFUNCTION wolfBoot_start(void) /* Use local version parser to avoid double-decryption issue * when EXT_ENCRYPTED && MMU is also defined */ pB_ver = get_decrypted_blob_version(dec_hdr); + slot_note_image(&boot_slots[1], dec_hdr); } #else pB_ver = wolfBoot_get_blob_version((uint8_t*)p_hdr); + slot_note_image(&boot_slots[1], p_hdr); #endif } @@ -1294,6 +1374,11 @@ void RAMFUNCTION wolfBoot_start(void) * disk_close(BOOT_DISK) is deferred to just before hal_prepare_boot(). */ wolfBoot_printf("Firmware Valid.\r\n"); disk_boot_part = (int)boot_slots[selected].part; +#if defined(WOLFBOOT_DISK_FS) && defined(DISK_BOOT_CONFIRM) + if (boot_slots[selected].vol.type != FS_TYPE_RAW) { + disk_boot_file = boot_slots[selected].file; + } +#endif #ifdef DISK_BOOT_CONFIRM /* Put the slot on probation, but only if an update was staged into it. diff --git a/tools/unit-tests/Makefile b/tools/unit-tests/Makefile index 900e7d32cb..72b3d77eb6 100644 --- a/tools/unit-tests/Makefile +++ b/tools/unit-tests/Makefile @@ -223,7 +223,7 @@ TESTS+=unit-p1021-qe-firmware TESTS+=unit-t10xx-dts-memac TESTS+=unit-fs-probe TESTS+=unit-fs-malicious -TESTS+=unit-update-disk-fs unit-update-disk-fs-enc +TESTS+=unit-update-disk-fs unit-update-disk-fs-enc unit-update-disk-fs-confirm # The filesystem interop tests build their fixtures with the host's real # mkfs tools, so they are only added when those tools exist. The hostile @@ -631,6 +631,12 @@ unit-update-disk-fs-enc:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED \ -DEXT_ENCRYPTED -DENCRYPT_WITH_CHACHA -DHAVE_CHACHA \ -DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT \ -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE +unit-update-disk-fs-confirm:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED \ + -DWOLFBOOT_RAMBOOT_MAX_SIZE=0x1000 -DMAX_DISKS=1 \ + -DWOLFBOOT_DISK_FS -DWOLFBOOT_FAT32 -DWOLFBOOT_EXT4 \ + -DDISK_BOOT_CONFIRM \ + -DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT \ + -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE unit-update-disk-fs:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED \ -DWOLFBOOT_RAMBOOT_MAX_SIZE=0x1000 -DMAX_DISKS=1 \ -DWOLFBOOT_DISK_FS -DWOLFBOOT_FAT32 -DWOLFBOOT_EXT4 \ @@ -1545,6 +1551,12 @@ unit-update-disk-fs: ../../include/target.h unit-update-disk-fs.c unit-update-disk-fs-enc: ../../include/target.h unit-update-disk-fs.c gcc -o $@ unit-update-disk-fs.c $(CFLAGS) $(LDFLAGS) +# Same source with boot confirmation compiled in: the trailer of a file slot +# is the tail of its image file, located through the filesystem and rewritten +# in place. +unit-update-disk-fs-confirm: ../../include/target.h unit-update-disk-fs.c + gcc -o $@ unit-update-disk-fs.c $(CFLAGS) $(LDFLAGS) + unit-update-disk-fit: ../../include/target.h unit-update-disk-fit.c gcc -o $@ unit-update-disk-fit.c $(CFLAGS) $(LDFLAGS) diff --git a/tools/unit-tests/unit-update-disk-fs.c b/tools/unit-tests/unit-update-disk-fs.c index 6bfd942a1d..86ff390c4f 100644 --- a/tools/unit-tests/unit-update-disk-fs.c +++ b/tools/unit-tests/unit-update-disk-fs.c @@ -138,10 +138,16 @@ int disk_read(int drv, uint64_t start, uint32_t count, uint8_t *buf) return 0; } +static int mock_disk_write_count; + int disk_write(int drv, uint64_t start, uint32_t count, const uint8_t *buf) { - (void)drv; (void)start; (void)count; (void)buf; - return -1; + (void)drv; + mock_disk_write_count++; + if ((start + count) > DISK_SZ) + return -1; + memcpy(disk + start, buf, count); + return 0; } int disk_init(int drv) @@ -625,6 +631,7 @@ static void reset_mocks(void) memset(load_buffer, 0, sizeof(load_buffer)); mock_disk_init_ret = 0; mock_disk_close_called = 0; + mock_disk_write_count = 0; mock_do_boot_called = 0; mock_boot_address = NULL; mock_verify_integrity_ret = 0; @@ -1094,6 +1101,202 @@ START_TEST(test_fs_raw_slot_without_boot_file_still_reads) } END_TEST +#ifdef DISK_BOOT_CONFIRM +/* Boot confirmation on file slots: the trailer is the last bytes of the + * image file, which is padded past the signed image to make room. */ +#define CONFIRM_PAYLOAD_SIZE 2048U +#define CONFIRM_IMAGE_SIZE (IMAGE_HEADER_SIZE + CONFIRM_PAYLOAD_SIZE) +#define PADDED_SIZE (CONFIRM_IMAGE_SIZE + 1024U) + +static uint64_t fat_tail(uint32_t file_size) +{ + return P0_OFF + F_CLUS(F_FILE_CLUS) + file_size - DISK_TRAILER_SZ; +} + +static uint64_t ext_tail(uint32_t file_size) +{ + return P1_OFF + ((uint64_t)E_FILE_BLK * E_BLK) + file_size - + DISK_TRAILER_SZ; +} + +/* Smaller images than the other tests use, so image plus padding still fits + * the load bound. ver_a picks which slot is newer; slot B is version 2. */ +static void build_padded(uint32_t ver_a) +{ + build_image(image_a, ver_a, 0xA1, CONFIRM_PAYLOAD_SIZE); + build_image(image_b, 2, 0xB2, CONFIRM_PAYLOAD_SIZE); + build_fat32(PADDED_SIZE); + build_ext4(PADDED_SIZE); +} + +/* A staged file boots and its trailer, at tail, becomes TESTING with one + * write that leaves the image, at file, as it was. */ +static void check_arms_staged(uint32_t ver_a, uint64_t tail, + const uint8_t *image, uint64_t file, const uint8_t *media) +{ + reset_mocks(); + build_padded(ver_a); + disk_trailer_encode(disk + tail, DISK_STATE_UPDATING); + + wolfBoot_start(); + + ck_assert_int_eq(wolfBoot_panicked, 0); + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_int_eq(memcmp(load_buffer, image + IMAGE_HEADER_SIZE, + CONFIRM_PAYLOAD_SIZE), 0); + ck_assert_int_eq(mock_disk_write_count, 1); + ck_assert_uint_eq(disk_trailer_decode(disk + tail), DISK_STATE_TESTING); + ck_assert_int_eq(memcmp(disk + file, media, CONFIRM_IMAGE_SIZE), 0); +} + +START_TEST(test_fs_confirm_arms_staged_ext4_file) +{ + check_arms_staged(1, ext_tail(PADDED_SIZE), image_b, + P1_OFF + ((uint64_t)E_FILE_BLK * E_BLK), media_b); +} +END_TEST + +START_TEST(test_fs_confirm_arms_staged_fat32_file) +{ + check_arms_staged(9, fat_tail(PADDED_SIZE), image_a, + P0_OFF + F_CLUS(F_FILE_CLUS), media_a); +} +END_TEST + +/* A file still TESTING was armed before the last boot and never confirmed: + * it is skipped and the older slot boots, with nothing written. */ +START_TEST(test_fs_confirm_skips_unconfirmed_file) +{ + reset_mocks(); + build_padded(1); + disk_trailer_encode(disk + ext_tail(PADDED_SIZE), DISK_STATE_TESTING); + + wolfBoot_start(); + + ck_assert_int_eq(wolfBoot_panicked, 0); + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_int_eq(memcmp(load_buffer, image_a + IMAGE_HEADER_SIZE, + CONFIRM_PAYLOAD_SIZE), 0); + ck_assert_int_eq(mock_disk_write_count, 0); +} +END_TEST + +START_TEST(test_fs_confirm_steady_state_writes_nothing) +{ + reset_mocks(); + build_padded(1); + disk_trailer_encode(disk + ext_tail(PADDED_SIZE), DISK_STATE_SUCCESS); + + wolfBoot_start(); + + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_int_eq(memcmp(load_buffer, image_b + IMAGE_HEADER_SIZE, + CONFIRM_PAYLOAD_SIZE), 0); + ck_assert_int_eq(mock_disk_write_count, 0); + + /* Never staged at all. */ + reset_mocks(); + build_padded(1); + wolfBoot_start(); + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_int_eq(mock_disk_write_count, 0); +} +END_TEST + +/* A file with no padding has its last bytes inside the image. Even if they + * happen to read as a staged trailer, the image must not be written to. */ +START_TEST(test_fs_confirm_never_writes_inside_the_image) +{ + reset_mocks(); + disk_trailer_encode(disk + ext_tail(TEST_IMAGE_SIZE), + DISK_STATE_UPDATING); + + wolfBoot_start(); + + ck_assert_int_eq(wolfBoot_panicked, 0); + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_int_eq(mock_disk_write_count, 0); + ck_assert_uint_eq(disk_trailer_decode(disk + ext_tail(TEST_IMAGE_SIZE)), + DISK_STATE_UPDATING); + + /* Nor may image bytes that read as TESTING take the slot out of the + * election: the newer slot B still boots. */ + reset_mocks(); + disk_trailer_encode(disk + ext_tail(TEST_IMAGE_SIZE), + DISK_STATE_TESTING); + + wolfBoot_start(); + + ck_assert_int_eq(wolfBoot_panicked, 0); + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_int_eq(slot_reaped[1], 0); + ck_assert_int_eq(mock_disk_write_count, 0); +} +END_TEST + +/* On a shared-blocks ext4 volume a file's block may be another file's too, + * and on a verity volume a Merkle tree may cover it, so it is never + * rewritten: the staged slot boots unarmed. */ +START_TEST(test_fs_confirm_shared_blocks_ext4_is_not_armed) +{ + static const uint32_t no_rewrite[] = { 0x4000U, 0x8000U }; + unsigned int i; + + for (i = 0; i < sizeof(no_rewrite) / sizeof(no_rewrite[0]); i++) { + reset_mocks(); + build_padded(1); + put32(P1_OFF + E_SB + 0x64, no_rewrite[i]); + disk_trailer_encode(disk + ext_tail(PADDED_SIZE), + DISK_STATE_UPDATING); + + wolfBoot_start(); + + ck_assert_int_eq(wolfBoot_panicked, 0); + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_int_eq(memcmp(load_buffer, image_b + IMAGE_HEADER_SIZE, + CONFIRM_PAYLOAD_SIZE), 0); + ck_assert_int_eq(mock_disk_write_count, 0); + ck_assert_uint_eq(disk_trailer_decode(disk + ext_tail(PADDED_SIZE)), + DISK_STATE_UPDATING); + } +} +END_TEST + +/* A trailer that straddles two clusters which are not neighbours on the + * media is not one run, so the slot cannot carry a state. */ +START_TEST(test_fs_confirm_split_tail_is_not_armed) +{ + uint64_t off = 0; + /* Four bytes past a cluster boundary, so the trailer spans it. */ + uint32_t size = (6U * SEC) + 4U; + uint8_t staged[DISK_TRAILER_SZ]; + uint64_t head = P0_OFF + F_CLUS(F_FILE_CLUS + (5U * F_FRAG_STRIDE)) + + SEC - 4U; + uint64_t tail = P0_OFF + F_CLUS(F_FILE_CLUS + (6U * F_FRAG_STRIDE)); + + reset_mocks(); + build_image(image_a, 9, 0xA1, CONFIRM_PAYLOAD_SIZE); + build_fat32_fragmented(size); + /* Staged, in the two clusters the file's last eight bytes live in. */ + disk_trailer_encode(staged, DISK_STATE_UPDATING); + memcpy(disk + head, staged, 4); + memcpy(disk + tail, staged + 4, 4); + + wolfBoot_start(); + + ck_assert_int_eq(wolfBoot_panicked, 0); + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_int_eq(memcmp(load_buffer, image_a + IMAGE_HEADER_SIZE, + CONFIRM_PAYLOAD_SIZE), 0); + ck_assert_int_eq(mock_disk_write_count, 0); + ck_assert_int_eq(memcmp(disk + head, staged, 4), 0); + ck_assert_int_eq(memcmp(disk + tail, staged + 4, 4), 0); + ck_assert_int_eq(fs_map(&boot_slots[0].f, size - DISK_TRAILER_SZ, + DISK_TRAILER_SZ, &off), WOLFBOOT_FS_E_UNSUPPORTED); +} +END_TEST +#endif /* DISK_BOOT_CONFIRM */ + static Suite *wolfboot_suite(void) { Suite *s = suite_create("wolfboot-update-disk-fs"); @@ -1125,6 +1328,25 @@ static Suite *wolfboot_suite(void) tcase_add_test(tc_slot, test_fs_raw_slot_without_boot_file_still_reads); suite_add_tcase(s, tc_slot); +#ifdef DISK_BOOT_CONFIRM + { + TCase *tc_confirm = tcase_create("update-disk-fs-confirm"); + + tcase_set_timeout(tc_confirm, 60); + tcase_add_test(tc_confirm, test_fs_confirm_arms_staged_ext4_file); + tcase_add_test(tc_confirm, test_fs_confirm_arms_staged_fat32_file); + tcase_add_test(tc_confirm, test_fs_confirm_skips_unconfirmed_file); + tcase_add_test(tc_confirm, + test_fs_confirm_steady_state_writes_nothing); + tcase_add_test(tc_confirm, + test_fs_confirm_never_writes_inside_the_image); + tcase_add_test(tc_confirm, test_fs_confirm_split_tail_is_not_armed); + tcase_add_test(tc_confirm, + test_fs_confirm_shared_blocks_ext4_is_not_armed); + suite_add_tcase(s, tc_confirm); + } +#endif + return s; } From ecda17a50e16f314e184e0dc1325490cc2d35ee9 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 12 Nov 2026 17:57:12 -0800 Subject: [PATCH 7/9] Report whether a FIT ramdisk was loaded --- include/fdt.h | 5 +++++ src/fdt.c | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/include/fdt.h b/include/fdt.h index 5b03854e25..6f0c26aa8b 100644 --- a/include/fdt.h +++ b/include/fdt.h @@ -425,6 +425,11 @@ void* fit_load_kernel(fdt_ctx* ctx, const char* kernel_node, int* lenp); * `dts`. Returns 0 on success, -1 on load failure or when `dts` is NULL, * since a staged ramdisk is useless unless the tree names it. */ int fit_load_ramdisk(fdt_ctx* ctx, const char* ramdisk_node, fdt_ctx* dts); + +/* 1 once fit_load_ramdisk() has staged a ramdisk and written its range to + * the device tree, so a later fixup pass can tell that range from one the + * tree already carried. */ +int fit_ramdisk_loaded(void); #endif #ifdef WOLFBOOT_FPGA_BITSTREAM diff --git a/src/fdt.c b/src/fdt.c index 174a2ee93d..45b239cffe 100644 --- a/src/fdt.c +++ b/src/fdt.c @@ -1725,6 +1725,13 @@ void* fit_load_kernel(fdt_ctx* ctx, const char* kernel_node, int* lenp) #define WOLFBOOT_FIT_MAX_RAMDISK WOLFBOOT_FIT_MAX_DECOMP #endif +static int fit_ramdisk_ok; + +int fit_ramdisk_loaded(void) +{ + return fit_ramdisk_ok; +} + /* Load a FIT ramdisk subimage and patch the DTB's /chosen * linux,initrd-{start,end} to point at it. If * WOLFBOOT_LOAD_RAMDISK_ADDRESS is nonzero, the ramdisk is loaded @@ -1786,6 +1793,7 @@ int fit_load_ramdisk(fdt_ctx* ctx, const char* ramdisk_node, fdt_ctx* dts) } } + fit_ramdisk_ok = 1; return 0; } #endif /* WOLFBOOT_FIT_RAMDISK */ From 854bf0f643146d8285642e40ff90ca087dd4282d Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 16 Nov 2026 10:31:54 -0800 Subject: [PATCH 8/9] Add a minimal PSCI 1.1 runtime for firmware that stays resident at EL3 --- .gitignore | 1 + include/psci.h | 100 +++++++++++ src/psci.c | 234 +++++++++++++++++++++++++ tools/unit-tests/Makefile | 4 + tools/unit-tests/unit-psci.c | 324 +++++++++++++++++++++++++++++++++++ 5 files changed, 663 insertions(+) create mode 100644 include/psci.h create mode 100644 src/psci.c create mode 100644 tools/unit-tests/unit-psci.c diff --git a/.gitignore b/.gitignore index f4bc059885..6db2f45ae0 100644 --- a/.gitignore +++ b/.gitignore @@ -157,6 +157,7 @@ tools/unit-tests/unit-ext4 tools/unit-tests/unit-update-disk-fs tools/unit-tests/unit-update-disk-fs-enc tools/unit-tests/unit-update-disk-fs-confirm +tools/unit-tests/unit-psci tools/unit-tests/ext4-min.img tools/unit-tests/ext4-stock.img tools/unit-tests/ext4-payload.bin diff --git a/include/psci.h b/include/psci.h new file mode 100644 index 0000000000..19f2e3fc73 --- /dev/null +++ b/include/psci.h @@ -0,0 +1,100 @@ +/* psci.h + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +/* Interface between the EL3 PSCI runtime (src/psci.c) and the platform HAL + * that owns the SMC vector and the cores. */ + +#ifndef PSCI_H +#define PSCI_H + +#include + +#ifndef WOLFBOOT_PSCI_NUM_CPUS +#define WOLFBOOT_PSCI_NUM_CPUS 4 +#endif + +/* SMCCC fast calls: bit 31 fast, bit 30 SMC64, bits 29:24 owner */ +#define SMCCC_SMC64 0x40000000UL +#define SMCCC_VERSION 0x80000000UL +#define SMCCC_ARCH_FEATURES 0x80000001UL +#define SMCCC_VERSION_1_1 0x00010001UL + +#define PSCI_VERSION 0x84000000UL +#define PSCI_CPU_SUSPEND 0x84000001UL +#define PSCI_CPU_OFF 0x84000002UL +#define PSCI_CPU_ON 0x84000003UL +#define PSCI_AFFINITY_INFO 0x84000004UL +#define PSCI_MIGRATE_INFO_TYPE 0x84000006UL +#define PSCI_SYSTEM_OFF 0x84000008UL +#define PSCI_SYSTEM_RESET 0x84000009UL +#define PSCI_FEATURES 0x8400000AUL +#define PSCI_VERSION_1_1 0x00010001UL + +/* MIGRATE_INFO_TYPE: no Trusted OS, migration not required */ +#define PSCI_TOS_NOT_PRESENT 2 + +#define PSCI_E_SUCCESS 0 +#define PSCI_E_NOT_SUPPORTED (-1) +#define PSCI_E_INVALID_PARAMETERS (-2) +#define PSCI_E_DENIED (-3) +#define PSCI_E_ALREADY_ON (-4) +#define PSCI_E_ON_PENDING (-5) +#define PSCI_E_INTERNAL_FAILURE (-6) +#define PSCI_E_INVALID_ADDRESS (-9) + +/* AFFINITY_INFO results */ +#define PSCI_STATE_ON 0 +#define PSCI_STATE_OFF 1 +#define PSCI_STATE_ON_PENDING 2 + +/* Before any secondary core runs: the boot core is on, the others off. */ +void wolfBoot_psci_init(unsigned int boot_cpu); + +/* One SMC from a lower exception level, x0-x3 as received; the result goes + * back in x0. */ +uint64_t wolfBoot_psci_smc(uint64_t fid, uint64_t a1, uint64_t a2, + uint64_t a3); + +/* Called by a parked core. Returns 1 with the entry point and context id + * once CPU_ON has been issued for it, 0 while it must keep waiting. */ +int wolfBoot_psci_cpu_take(unsigned int cpu, uint64_t* entry, + uint64_t* context); + +/* Non-zero once the core has parked for the first time. */ +int wolfBoot_psci_cpu_alive(unsigned int cpu); + +/* Platform hooks. All are called at EL3 on the core that issued the SMC. */ + +/* Linear core index for an MPIDR as passed to CPU_ON, negative if it does + * not name a core. */ +int hal_psci_mpidr_to_cpu(uint64_t mpidr); +unsigned int hal_psci_this_cpu(void); +/* Non-zero if a lower exception level may be entered at this address. */ +int hal_psci_entry_ok(uint64_t entry); +/* Wake a parked core so that it calls wolfBoot_psci_cpu_take() again. */ +void hal_psci_cpu_wake(unsigned int cpu); +/* Park the calling core. Does not return on success. */ +void hal_psci_cpu_off(void); +/* Neither returns on success. */ +void hal_psci_system_reset(void); +void hal_psci_system_off(void); + +#endif /* PSCI_H */ diff --git a/src/psci.c b/src/psci.c new file mode 100644 index 0000000000..5a5d273bda --- /dev/null +++ b/src/psci.c @@ -0,0 +1,234 @@ +/* psci.c + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +/* Minimal PSCI 1.1 runtime for a wolfBoot that stays resident at EL3 after + * the handoff: core power on and off, affinity state, system reset and off. + * The platform HAL owns the SMC vector and calls wolfBoot_psci_smc(). It + * runs on the caller's EL3 stack with the caller's FP/SIMD state live, so + * this file must be built without FP/SIMD register use. */ + +#include +#include +#include "psci.h" + +struct psci_cpu { + uint64_t entry; + uint64_t context; + volatile uint8_t state; + volatile uint8_t alive; +}; + +static struct psci_cpu psci_cpus[WOLFBOOT_PSCI_NUM_CPUS]; +static volatile unsigned char psci_lock; + +static void psci_acquire(void) +{ + while (__atomic_test_and_set(&psci_lock, __ATOMIC_ACQUIRE)) { + /* wait on a plain read, so that waiters do not take the line from + * the holder with a store per attempt */ + while (psci_lock) { + } + } +} + +static void psci_release(void) +{ + __atomic_clear(&psci_lock, __ATOMIC_RELEASE); +} + +void wolfBoot_psci_init(unsigned int boot_cpu) +{ + unsigned int i; + + for (i = 0; i < WOLFBOOT_PSCI_NUM_CPUS; i++) { + psci_cpus[i].entry = 0; + psci_cpus[i].context = 0; + psci_cpus[i].state = PSCI_STATE_OFF; + psci_cpus[i].alive = 0; + } + if (boot_cpu < WOLFBOOT_PSCI_NUM_CPUS) { + psci_cpus[boot_cpu].state = PSCI_STATE_ON; + psci_cpus[boot_cpu].alive = 1; + } + psci_lock = 0; +} + +int wolfBoot_psci_cpu_take(unsigned int cpu, uint64_t* entry, + uint64_t* context) +{ + int ret = 0; + + if (cpu >= WOLFBOOT_PSCI_NUM_CPUS || entry == NULL || context == NULL) + return 0; + psci_cpus[cpu].alive = 1; + /* Unlocked first look: the lock's exclusive accesses would wake this + * core out of its own wait-for-event. */ + if (psci_cpus[cpu].state != PSCI_STATE_ON_PENDING) + return 0; + psci_acquire(); + if (psci_cpus[cpu].state == PSCI_STATE_ON_PENDING) { + psci_cpus[cpu].state = PSCI_STATE_ON; + *entry = psci_cpus[cpu].entry; + *context = psci_cpus[cpu].context; + ret = 1; + } + psci_release(); + return ret; +} + +int wolfBoot_psci_cpu_alive(unsigned int cpu) +{ + if (cpu >= WOLFBOOT_PSCI_NUM_CPUS) + return 0; + return psci_cpus[cpu].alive; +} + +static int psci_cpu_on(uint64_t mpidr, uint64_t entry, uint64_t context) +{ + int cpu = hal_psci_mpidr_to_cpu(mpidr); + int ret; + + if (cpu < 0 || cpu >= WOLFBOOT_PSCI_NUM_CPUS) + return PSCI_E_INVALID_PARAMETERS; + if (!hal_psci_entry_ok(entry)) + return PSCI_E_INVALID_ADDRESS; + psci_acquire(); + if (psci_cpus[cpu].state == PSCI_STATE_ON) { + ret = PSCI_E_ALREADY_ON; + } + else if (psci_cpus[cpu].state == PSCI_STATE_ON_PENDING) { + ret = PSCI_E_ON_PENDING; + } + else if (!psci_cpus[cpu].alive) { + /* never reached its parking loop: nothing would answer the wake */ + ret = PSCI_E_INTERNAL_FAILURE; + } + else { + psci_cpus[cpu].entry = entry; + psci_cpus[cpu].context = context; + psci_cpus[cpu].state = PSCI_STATE_ON_PENDING; + ret = PSCI_E_SUCCESS; + } + psci_release(); + if (ret == PSCI_E_SUCCESS) + hal_psci_cpu_wake((unsigned int)cpu); + return ret; +} + +static int psci_cpu_off(void) +{ + unsigned int cpu = hal_psci_this_cpu(); + + if (cpu >= WOLFBOOT_PSCI_NUM_CPUS) + return PSCI_E_DENIED; + psci_acquire(); + psci_cpus[cpu].state = PSCI_STATE_OFF; + psci_release(); + hal_psci_cpu_off(); + /* still running: the core could not be parked */ + psci_acquire(); + psci_cpus[cpu].state = PSCI_STATE_ON; + psci_release(); + return PSCI_E_DENIED; +} + +static int psci_affinity_info(uint64_t mpidr, uint64_t level) +{ + int cpu = hal_psci_mpidr_to_cpu(mpidr); + int ret; + + if (level != 0 || cpu < 0 || cpu >= WOLFBOOT_PSCI_NUM_CPUS) + return PSCI_E_INVALID_PARAMETERS; + psci_acquire(); + ret = psci_cpus[cpu].state; + psci_release(); + return ret; +} + +static int psci_features(uint64_t fid) +{ + switch (fid) { + case PSCI_VERSION: + case PSCI_CPU_OFF: + case PSCI_CPU_ON: + case PSCI_CPU_ON | SMCCC_SMC64: + case PSCI_AFFINITY_INFO: + case PSCI_AFFINITY_INFO | SMCCC_SMC64: + case PSCI_MIGRATE_INFO_TYPE: + case PSCI_SYSTEM_OFF: + case PSCI_SYSTEM_RESET: + case PSCI_FEATURES: + case SMCCC_VERSION: + return PSCI_E_SUCCESS; + default: + return PSCI_E_NOT_SUPPORTED; + } +} + +uint64_t wolfBoot_psci_smc(uint64_t fid, uint64_t a1, uint64_t a2, + uint64_t a3) +{ + int ret; + + /* w0 carries the function id; an SMC32 call passes 32-bit arguments */ + fid &= 0xFFFFFFFFUL; + if ((fid & SMCCC_SMC64) == 0) { + a1 &= 0xFFFFFFFFUL; + a2 &= 0xFFFFFFFFUL; + a3 &= 0xFFFFFFFFUL; + } + switch (fid) { + case SMCCC_VERSION: + return SMCCC_VERSION_1_1; + case PSCI_VERSION: + return PSCI_VERSION_1_1; + case PSCI_FEATURES: + ret = psci_features(a1); + break; + case PSCI_CPU_ON: + case PSCI_CPU_ON | SMCCC_SMC64: + ret = psci_cpu_on(a1, a2, a3); + break; + case PSCI_CPU_OFF: + ret = psci_cpu_off(); + break; + case PSCI_AFFINITY_INFO: + case PSCI_AFFINITY_INFO | SMCCC_SMC64: + ret = psci_affinity_info(a1, a2); + break; + case PSCI_MIGRATE_INFO_TYPE: + ret = PSCI_TOS_NOT_PRESENT; + break; + case PSCI_SYSTEM_RESET: + hal_psci_system_reset(); + ret = PSCI_E_INTERNAL_FAILURE; + break; + case PSCI_SYSTEM_OFF: + hal_psci_system_off(); + ret = PSCI_E_INTERNAL_FAILURE; + break; + default: + /* also the SMCCC "unknown function" value */ + ret = PSCI_E_NOT_SUPPORTED; + break; + } + return (uint64_t)(int64_t)ret; +} diff --git a/tools/unit-tests/Makefile b/tools/unit-tests/Makefile index 72b3d77eb6..1c657f925c 100644 --- a/tools/unit-tests/Makefile +++ b/tools/unit-tests/Makefile @@ -188,6 +188,7 @@ TESTS+=unit-rp2350-flash-write TESTS+=unit-fwtpm-rsp-overrun TESTS+=unit-fwtpm-cmd-toctou TESTS+=unit-fdt-memrsv-wrap +TESTS+=unit-psci TESTS+=unit-fdt-addr32 TESTS+=unit-ns16550 TESTS+=unit-zynq-dma-range @@ -876,6 +877,9 @@ unit-store-sbrk: unit-store-sbrk.c ../../src/store_sbrk.c unit-string: ../../include/target.h unit-string.c gcc -o $@ $^ $(CFLAGS) -DDEBUG_UART -DPRINTF_ENABLED $(LDFLAGS) +unit-psci: ../../include/target.h unit-psci.c ../../src/psci.c + gcc -o $@ unit-psci.c $(CFLAGS) $(LDFLAGS) + unit-arm-tee-psa-ipc: ../../include/target.h unit-arm-tee-psa-ipc.c ../../src/arm_tee_psa_ipc.c gcc -o $@ unit-arm-tee-psa-ipc.c $(CFLAGS) -I$(WOLFBOOT_LIB_WOLFPSA)/wolfpsa \ -ffunction-sections -fdata-sections \ diff --git a/tools/unit-tests/unit-psci.c b/tools/unit-tests/unit-psci.c new file mode 100644 index 0000000000..e498e24337 --- /dev/null +++ b/tools/unit-tests/unit-psci.c @@ -0,0 +1,324 @@ +/* unit-psci.c + * + * Unit tests for the EL3 PSCI runtime (src/psci.c). + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include + +#include "psci.c" + +#define RAM_BASE 0x80000000ULL +#define RAM_SIZE 0x80000000ULL +#define ENTRY (RAM_BASE + 0x80000) +#define ERR(e) ((uint64_t)(int64_t)(e)) + +/* Platform side: two clusters of two cores, as on the S32G2. */ +static unsigned int this_cpu; +static int woken_cpu; +static int reset_calls; +static int off_calls; +static uint64_t state_in_cpu_off; + +int hal_psci_mpidr_to_cpu(uint64_t mpidr) +{ + if ((mpidr & ~0x101ULL) != 0) + return -1; + return (int)(((mpidr >> 8) & 1) * 2 + (mpidr & 1)); +} + +unsigned int hal_psci_this_cpu(void) +{ + return this_cpu; +} + +int hal_psci_entry_ok(uint64_t entry) +{ + return ((entry & 3) == 0 && entry >= RAM_BASE && + entry - RAM_BASE < RAM_SIZE); +} + +void hal_psci_cpu_wake(unsigned int cpu) +{ + woken_cpu = (int)cpu; +} + +/* The real hook does not return; this one records what the other cores + * would see while the core parks. */ +void hal_psci_cpu_off(void) +{ + uint64_t mpidr = ((uint64_t)(this_cpu >> 1) << 8) | (this_cpu & 1); + + state_in_cpu_off = wolfBoot_psci_smc(PSCI_AFFINITY_INFO | SMCCC_SMC64, + mpidr, 0, 0); +} + +void hal_psci_system_reset(void) +{ + reset_calls++; +} + +void hal_psci_system_off(void) +{ + off_calls++; +} + +static void setup(void) +{ + this_cpu = 0; + woken_cpu = -1; + reset_calls = 0; + off_calls = 0; + state_in_cpu_off = ~0ULL; + wolfBoot_psci_init(0); +} + +/* Park a core as its startup code does, so that CPU_ON can reach it. */ +static void park(unsigned int cpu) +{ + uint64_t entry, context; + + ck_assert_int_eq(wolfBoot_psci_cpu_take(cpu, &entry, &context), 0); +} + +START_TEST(test_version_and_features) +{ + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_VERSION, 0, 0, 0), + PSCI_VERSION_1_1); + ck_assert_uint_eq(wolfBoot_psci_smc(SMCCC_VERSION, 0, 0, 0), + SMCCC_VERSION_1_1); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_MIGRATE_INFO_TYPE, 0, 0, 0), + PSCI_TOS_NOT_PRESENT); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_FEATURES, + PSCI_CPU_ON | SMCCC_SMC64, 0, 0), PSCI_E_SUCCESS); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_FEATURES, PSCI_SYSTEM_RESET, + 0, 0), PSCI_E_SUCCESS); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_FEATURES, SMCCC_VERSION, 0, 0), + PSCI_E_SUCCESS); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_FEATURES, PSCI_CPU_SUSPEND, + 0, 0), ERR(PSCI_E_NOT_SUPPORTED)); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_FEATURES, SMCCC_ARCH_FEATURES, + 0, 0), ERR(PSCI_E_NOT_SUPPORTED)); +} +END_TEST + +START_TEST(test_unknown_calls) +{ + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_CPU_SUSPEND, 0, ENTRY, 0), + ERR(PSCI_E_NOT_SUPPORTED)); + ck_assert_uint_eq(wolfBoot_psci_smc(SMCCC_ARCH_FEATURES, 0x80008000UL, + 0, 0), ERR(PSCI_E_NOT_SUPPORTED)); + /* a yielding call, and another owner's range */ + ck_assert_uint_eq(wolfBoot_psci_smc(0x04000003UL, 1, ENTRY, 0), + ERR(PSCI_E_NOT_SUPPORTED)); + ck_assert_uint_eq(wolfBoot_psci_smc(0xC2000001UL, 0, 0, 0), + ERR(PSCI_E_NOT_SUPPORTED)); + /* only w0 is the function id */ + ck_assert_uint_eq(wolfBoot_psci_smc(0xFFFFFFFF00000000ULL | PSCI_VERSION, + 0, 0, 0), PSCI_VERSION_1_1); + ck_assert_int_eq(woken_cpu, -1); +} +END_TEST + +START_TEST(test_cpu_on_arguments) +{ + uint64_t on = PSCI_CPU_ON | SMCCC_SMC64; + + park(1); + /* not a core: a third core in a cluster, a third cluster, aff2, aff3 */ + ck_assert_uint_eq(wolfBoot_psci_smc(on, 0x2, ENTRY, 0), + ERR(PSCI_E_INVALID_PARAMETERS)); + ck_assert_uint_eq(wolfBoot_psci_smc(on, 0x200, ENTRY, 0), + ERR(PSCI_E_INVALID_PARAMETERS)); + ck_assert_uint_eq(wolfBoot_psci_smc(on, 0x10001, ENTRY, 0), + ERR(PSCI_E_INVALID_PARAMETERS)); + ck_assert_uint_eq(wolfBoot_psci_smc(on, 0x100000001ULL, ENTRY, 0), + ERR(PSCI_E_INVALID_PARAMETERS)); + /* entry points outside RAM, in the firmware's own memory, unaligned */ + ck_assert_uint_eq(wolfBoot_psci_smc(on, 1, 0, 0), + ERR(PSCI_E_INVALID_ADDRESS)); + ck_assert_uint_eq(wolfBoot_psci_smc(on, 1, 0x340A0000, 0), + ERR(PSCI_E_INVALID_ADDRESS)); + ck_assert_uint_eq(wolfBoot_psci_smc(on, 1, RAM_BASE + RAM_SIZE, 0), + ERR(PSCI_E_INVALID_ADDRESS)); + ck_assert_uint_eq(wolfBoot_psci_smc(on, 1, ENTRY + 2, 0), + ERR(PSCI_E_INVALID_ADDRESS)); + ck_assert_int_eq(woken_cpu, -1); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_AFFINITY_INFO, 1, 0, 0), + PSCI_STATE_OFF); +} +END_TEST + +START_TEST(test_cpu_on_states) +{ + uint64_t on = PSCI_CPU_ON | SMCCC_SMC64; + uint64_t entry = 0, context = 0; + + /* the calling core */ + ck_assert_uint_eq(wolfBoot_psci_smc(on, 0, ENTRY, 0), + ERR(PSCI_E_ALREADY_ON)); + /* a core that never reached its parking loop */ + ck_assert_uint_eq(wolfBoot_psci_smc(on, 0x100, ENTRY, 0), + ERR(PSCI_E_INTERNAL_FAILURE)); + ck_assert_int_eq(wolfBoot_psci_cpu_alive(2), 0); + ck_assert_int_eq(woken_cpu, -1); + + park(2); + ck_assert_int_eq(wolfBoot_psci_cpu_alive(2), 1); + ck_assert_uint_eq(wolfBoot_psci_smc(on, 0x100, ENTRY, 0x1234), + PSCI_E_SUCCESS); + ck_assert_int_eq(woken_cpu, 2); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_AFFINITY_INFO | SMCCC_SMC64, + 0x100, 0, 0), PSCI_STATE_ON_PENDING); + ck_assert_uint_eq(wolfBoot_psci_smc(on, 0x100, ENTRY + 4, 0), + ERR(PSCI_E_ON_PENDING)); + /* the other cores are not affected */ + ck_assert_int_eq(wolfBoot_psci_cpu_take(3, &entry, &context), 0); + + ck_assert_int_eq(wolfBoot_psci_cpu_take(2, &entry, &context), 1); + ck_assert_uint_eq(entry, ENTRY); + ck_assert_uint_eq(context, 0x1234); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_AFFINITY_INFO, 0x100, 0, 0), + PSCI_STATE_ON); + ck_assert_uint_eq(wolfBoot_psci_smc(on, 0x100, ENTRY, 0), + ERR(PSCI_E_ALREADY_ON)); + /* taken once */ + ck_assert_int_eq(wolfBoot_psci_cpu_take(2, &entry, &context), 0); +} +END_TEST + +START_TEST(test_cpu_on_smc32) +{ + uint64_t entry = 0, context = 0; + + park(1); + /* an SMC32 call passes 32-bit arguments: the upper halves are not part + * of them */ + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_CPU_ON, + 0xAAAAAAAA00000001ULL, 0xBBBBBBBB00000000ULL | ENTRY, + 0xCCCCCCCC00000042ULL), PSCI_E_SUCCESS); + ck_assert_int_eq(wolfBoot_psci_cpu_take(1, &entry, &context), 1); + ck_assert_uint_eq(entry, ENTRY); + ck_assert_uint_eq(context, 0x42); + /* the SMC64 form takes them whole */ + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_CPU_ON | SMCCC_SMC64, + 0xAAAAAAAA00000001ULL, ENTRY, 0), ERR(PSCI_E_INVALID_PARAMETERS)); +} +END_TEST + +START_TEST(test_affinity_info) +{ + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_AFFINITY_INFO, 0, 0, 0), + PSCI_STATE_ON); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_AFFINITY_INFO, 0x101, 0, 0), + PSCI_STATE_OFF); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_AFFINITY_INFO, 0, 1, 0), + ERR(PSCI_E_INVALID_PARAMETERS)); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_AFFINITY_INFO, 0x102, 0, 0), + ERR(PSCI_E_INVALID_PARAMETERS)); +} +END_TEST + +START_TEST(test_cpu_off_and_on_again) +{ + uint64_t on = PSCI_CPU_ON | SMCCC_SMC64; + uint64_t entry = 0, context = 0; + + park(3); + ck_assert_uint_eq(wolfBoot_psci_smc(on, 0x101, ENTRY, 7), PSCI_E_SUCCESS); + ck_assert_int_eq(wolfBoot_psci_cpu_take(3, &entry, &context), 1); + + /* The core is reported off from the moment it starts to park. The mock + * hook returns, which the runtime treats as a failed power down. */ + this_cpu = 3; + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_CPU_OFF, 0, 0, 0), + ERR(PSCI_E_DENIED)); + ck_assert_uint_eq(state_in_cpu_off, PSCI_STATE_OFF); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_AFFINITY_INFO, 0x101, 0, 0), + PSCI_STATE_ON); + + /* a core id the platform should never report */ + this_cpu = WOLFBOOT_PSCI_NUM_CPUS; + state_in_cpu_off = ~0ULL; + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_CPU_OFF, 0, 0, 0), + ERR(PSCI_E_DENIED)); + ck_assert_uint_eq(state_in_cpu_off, ~0ULL); +} +END_TEST + +START_TEST(test_system_calls) +{ + /* neither hook returns on hardware */ + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_SYSTEM_RESET, 0, 0, 0), + ERR(PSCI_E_INTERNAL_FAILURE)); + ck_assert_int_eq(reset_calls, 1); + ck_assert_int_eq(off_calls, 0); + ck_assert_uint_eq(wolfBoot_psci_smc(PSCI_SYSTEM_OFF, 0, 0, 0), + ERR(PSCI_E_INTERNAL_FAILURE)); + ck_assert_int_eq(off_calls, 1); +} +END_TEST + +START_TEST(test_take_arguments) +{ + uint64_t entry = 0, context = 0; + + ck_assert_int_eq(wolfBoot_psci_cpu_take(WOLFBOOT_PSCI_NUM_CPUS, &entry, + &context), 0); + ck_assert_int_eq(wolfBoot_psci_cpu_take(1, NULL, &context), 0); + ck_assert_int_eq(wolfBoot_psci_cpu_take(1, &entry, NULL), 0); + ck_assert_int_eq(wolfBoot_psci_cpu_alive(1), 0); + ck_assert_int_eq(wolfBoot_psci_cpu_alive(WOLFBOOT_PSCI_NUM_CPUS), 0); + ck_assert_int_eq(wolfBoot_psci_cpu_alive(0), 1); +} +END_TEST + +static Suite* psci_suite(void) +{ + Suite* s = suite_create("psci"); + TCase* tc = tcase_create("psci"); + + tcase_add_checked_fixture(tc, setup, NULL); + tcase_add_test(tc, test_version_and_features); + tcase_add_test(tc, test_unknown_calls); + tcase_add_test(tc, test_cpu_on_arguments); + tcase_add_test(tc, test_cpu_on_states); + tcase_add_test(tc, test_cpu_on_smc32); + tcase_add_test(tc, test_affinity_info); + tcase_add_test(tc, test_cpu_off_and_on_again); + tcase_add_test(tc, test_system_calls); + tcase_add_test(tc, test_take_arguments); + suite_add_tcase(s, tc); + return s; +} + +int main(void) +{ + int fails; + Suite* s = psci_suite(); + SRunner* sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + fails = srunner_ntests_failed(sr); + srunner_free(sr); + + return fails; +} From 199faabccfdf31955861f80005783742a6e5e715 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 12 Nov 2026 14:10:48 -0800 Subject: [PATCH 9/9] Add NXP S32G2 (S32G274A GoldBox) target: wolfBoot as the Boot ROM image, booting a signed FIT from SD, eMMC or QuadSPI NOR --- .github/workflows/test-configs.yml | 32 + .gitignore | 5 + Makefile | 12 + arch.mk | 51 + config/examples/s32g2-goldbox-qspi.config | 35 + config/examples/s32g2-goldbox.config | 33 + docs/Targets.md | 148 ++ hal/imx_usdhc_sdhci.c | 2 + hal/s32g2.c | 2057 +++++++++++++++++++++ hal/s32g2.h | 223 +++ hal/s32g2.its | 26 + hal/s32g2.ld | 74 + hal/s32g2_ddr.c | 486 +++++ src/boot_aarch64.c | 9 + src/boot_aarch64_start.S | 221 ++- tools/scripts/s32g2/s32g2-ddr-blob.py | 127 ++ tools/scripts/s32g2/s32g2-mkfit.sh | 42 + tools/scripts/s32g2/s32g2_mkimage.py | 148 ++ 18 files changed, 3730 insertions(+), 1 deletion(-) create mode 100644 config/examples/s32g2-goldbox-qspi.config create mode 100644 config/examples/s32g2-goldbox.config create mode 100644 hal/s32g2.c create mode 100644 hal/s32g2.h create mode 100644 hal/s32g2.its create mode 100644 hal/s32g2.ld create mode 100644 hal/s32g2_ddr.c create mode 100755 tools/scripts/s32g2/s32g2-ddr-blob.py create mode 100755 tools/scripts/s32g2/s32g2-mkfit.sh create mode 100755 tools/scripts/s32g2/s32g2_mkimage.py diff --git a/.github/workflows/test-configs.yml b/.github/workflows/test-configs.yml index 9ac4ee60f2..e239364d1c 100644 --- a/.github/workflows/test-configs.yml +++ b/.github/workflows/test-configs.yml @@ -944,6 +944,38 @@ jobs: config-file: ./config/examples/imx8mm-pico.config make-args: IMX8MM_BL33=1 DISK_EMMC=0 IMX8MM_MMU=0 wolfboot.bin test-app/image_v1_signed.bin + s32g2_test: + uses: ./.github/workflows/test-build-aarch64.yml + with: + arch: aarch64 + # Boot ROM image in SRAM, signed FIT from the SD card. + config-file: ./config/examples/s32g2-goldbox.config + make-args: wolfboot.bin + + s32g2_emmc_test: + uses: ./.github/workflows/test-build-aarch64.yml + with: + arch: aarch64 + # FIT from the eMMC behind the board's SW3, no console. + config-file: ./config/examples/s32g2-goldbox.config + make-args: DISK_EMMC=1 DISK_SDCARD=0 DEBUG_UART=0 wolfboot.bin + + s32g2_qspi_test: + uses: ./.github/workflows/test-build-aarch64.yml + with: + arch: aarch64 + # Boot from the QuadSPI NOR with the A/B partitions in the NOR. + config-file: ./config/examples/s32g2-goldbox-qspi.config + make-args: wolfboot.bin + + s32g2_qspi_program_test: + uses: ./.github/workflows/test-build-aarch64.yml + with: + arch: aarch64 + # NOR programmer build: image file from the SD card to the NOR. + config-file: ./config/examples/s32g2-goldbox.config + make-args: S32G2_QSPI=1 S32G2_QSPI_PROGRAM_FILE=/s32g2_qspi_boot.bin wolfboot.bin + disk_boot_confirm_test: uses: ./.github/workflows/test-build-aarch64.yml with: diff --git a/.gitignore b/.gitignore index 6db2f45ae0..9fa2ead21b 100644 --- a/.gitignore +++ b/.gitignore @@ -709,3 +709,8 @@ tools/unit-tests/stm32wb_write_extract.h tools/unit-tests/t10xx_flash_status_extract.h tools/unit-tests/ti_hercules_erase_extract.h tools/unit-tests/versal_host_write.c +# S32G2 GoldBox FIT build outputs (tools/scripts/s32g2/s32g2-mkfit.sh) +Image.gz +s32g2-fitImage* +s32g2.dtb +s32g2_ddr.bin diff --git a/Makefile b/Makefile index d0a57b07f2..2fbb46002d 100644 --- a/Makefile +++ b/Makefile @@ -515,6 +515,11 @@ ifeq ($(TARGET),imx8mm) MAIN_TARGET:=wolfboot.bin test-app/image_v1_signed.bin endif +# S32G2 runs from SRAM and boots a signed FIT; there is no factory.bin. +ifeq ($(TARGET),s32g2) + MAIN_TARGET:=wolfboot.bin +endif + # i.MX95 M7 runs from ITCM (loaded by the Linux remoteproc driver); the payload # lives in DDR at 0x80100000, so there is no contiguous flash image to assemble. ifeq ($(TARGET),imx95_m7) @@ -1101,6 +1106,13 @@ secondary: $(SECONDARY_PRIVATE_KEY) @echo "\t[CC $(ARCH)] $@" $(Q)$(CC) $(CFLAGS) -c $(OUTPUT_FLAG) $@ $^ +# hal/s32g2_ddr.o links the DDR blob in by content. +ifeq ($(TARGET),s32g2) +hal/s32g2_ddr.o: hal/s32g2_ddr.c $(wildcard $(S32G2_DDR_BLOB)) + @echo "\t[CC $(ARCH)] $@" + $(Q)$(CC) $(CFLAGS) -c $(OUTPUT_FLAG) $@ $< +endif + %.o:%.S @echo "\t[AS $(ARCH)] $@" $(Q)$(CC) $(CFLAGS) -c $(OUTPUT_FLAG) $@ $^ diff --git a/arch.mk b/arch.mk index de45f23dc3..6de94f652c 100644 --- a/arch.mk +++ b/arch.mk @@ -363,6 +363,57 @@ ifeq ($(ARCH),AARCH64) endif endif + ifeq ($(TARGET),s32g2) + # NXP S32G274A GoldBox: wolfBoot runs from SRAM at EL3 with its own + # identity MMU, and cleans the caches at the handoff. + ARCH_FLAGS=-mcpu=cortex-a53+crypto -march=armv8-a+crypto -mstrict-align + CFLAGS+=$(ARCH_FLAGS) -DCORTEX_A53 + CFLAGS+=-DWOLFBOOT_AARCH64_MMU_TEARDOWN + WOLFBOOT_ORIGIN=0x340A0000 + OBJS+=hal/s32g2_ddr.o + # PSCI runtime at EL3. An SMC is served with the kernel's FP/SIMD state + # live: no use of those registers, nor of libgcc's out-of-line atomics. + OBJS+=src/psci.o + hal/s32g2.o src/psci.o: CFLAGS+=-mgeneral-regs-only -mno-outline-atomics + # wolfBoot adds /memory and /psci to the FIT's DTB, which needs more + # room than the default headroom. + CFLAGS+=-DWOLFBOOT_FDT_FIXUP_HEADROOM=4096 + # LPDDR4 register tables and PHY training firmware, generated by + # tools/scripts/s32g2/s32g2-ddr-blob.py. Without the file the image + # still builds, and reports the missing blob instead of training. + S32G2_DDR_BLOB ?= s32g2_ddr.bin + ifneq ($(wildcard $(S32G2_DDR_BLOB)),) + CFLAGS+=-DS32G2_DDR_BLOB='"$(abspath $(S32G2_DDR_BLOB))"' + endif + ifneq ($(filter 1,$(DISK_SDCARD) $(DISK_EMMC)),) + # uSDHC -> SDHCI shim, shared with the i.MX 8 targets + OBJS+=hal/imx_usdhc_sdhci.o + # Card detect is a GPIO, so PRES_STATE bit 16 never sets. + CFLAGS+=-DSDHCI_FORCE_CARD_DETECT + # Read the payload in 64 KB chunks rather than the 512-byte default. + DISK_BLOCK_SIZE ?= 65536 + CFLAGS+=-DDISK_BLOCK_SIZE=$(DISK_BLOCK_SIZE) + # 3.3 V signalling only (no-1-8-v): high-speed rates. + ifeq ($(DISK_EMMC),1) + S32G2_USDHC_MAX_CLK_KHZ ?= 52000 + else + S32G2_USDHC_MAX_CLK_KHZ ?= 50000 + endif + CFLAGS+=-DS32G2_USDHC_MAX_CLK_KHZ=$(S32G2_USDHC_MAX_CLK_KHZ) + endif + # QuadSPI NOR driver (the ext_flash API with EXT_FLASH=1) + ifeq ($(S32G2_QSPI),1) + CFLAGS+=-DS32G2_QSPI + # NOR programmer build: writes the named file from the SD/eMMC FAT + # partition to the NOR at S32G2_QSPI_PROGRAM_OFFSET, then halts. + ifneq ($(S32G2_QSPI_PROGRAM_FILE),) + S32G2_QSPI_PROGRAM_OFFSET ?= 0 + CFLAGS+=-DS32G2_QSPI_PROGRAM_FILE='"$(S32G2_QSPI_PROGRAM_FILE)"' + CFLAGS+=-DS32G2_QSPI_PROGRAM_OFFSET=$(S32G2_QSPI_PROGRAM_OFFSET) + endif + endif + endif + ifeq ($(TARGET),imx8mm) # uSDHC -> SDHCI shim, shared with imx8qm ifneq ($(filter 1,$(DISK_SDCARD) $(DISK_EMMC)),) diff --git a/config/examples/s32g2-goldbox-qspi.config b/config/examples/s32g2-goldbox-qspi.config new file mode 100644 index 0000000000..fcadc1db6e --- /dev/null +++ b/config/examples/s32g2-goldbox-qspi.config @@ -0,0 +1,35 @@ +# NXP S32G2 GoldBox (S32G-VNP-GLDBOX, S32G274A-RDB2) - wolfBoot booting from +# the 64 MB QuadSPI NOR, with the A/B image partitions in the same NOR. +# Build, NOR layout and programming: docs/Targets.md. +# +# Never pass CFLAGS_EXTRA= on the command line - it replaces, not appends. +ARCH=AARCH64 +TARGET=s32g2 +S32G2_QSPI=1 +SIGN?=ECC384 +HASH?=SHA384 +DEBUG?=1 +DEBUG_UART?=1 +SPMATH?=1 +NO_XIP?=1 +EXT_FLASH?=1 +# The HAL sets up the GICv3 itself (hal/s32g2.c); skip the generic GICv2 init. +SKIP_GIC_INIT?=1 +GZIP?=1 +DISK_SDCARD?=0 +# The flash's 16-byte ECC units program once per erase: keep the partition +# state as a journal, in 64 KB sectors (docs/Targets.md). +NVM_FLASH_JOURNAL?=1 +WOLFBOOT_SECTOR_SIZE?=0x10000 +WOLFBOOT_PARTITION_SIZE?=0x1000000 +WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x100000 +WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x1100000 +WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x2100000 +# Separate DTB partitions are not used (the FIT carries the DTB) but the +# RAM-boot flow needs them defined; they sit after the swap sector. +WOLFBOOT_DTS_BOOT_ADDRESS?=0x2110000 +WOLFBOOT_DTS_UPDATE_ADDRESS?=0x2120000 + +WOLFBOOT_RAMBOOT_MAX_SIZE=0x4000000 +WOLFBOOT_LOAD_ADDRESS?=0xA0000000 +WOLFBOOT_LOAD_DTS_ADDRESS?=0xA5000000 diff --git a/config/examples/s32g2-goldbox.config b/config/examples/s32g2-goldbox.config new file mode 100644 index 0000000000..50cf0eb90d --- /dev/null +++ b/config/examples/s32g2-goldbox.config @@ -0,0 +1,33 @@ +# NXP S32G2 GoldBox (S32G-VNP-GLDBOX, S32G274A-RDB2) - wolfBoot as the Boot +# ROM's application image in SRAM, in place of U-Boot. The signed FIT is read +# from a FAT32 partition of the SD card, or of the eMMC with DISK_EMMC=1 +# DISK_SDCARD=0. Build, DDR blob and card layout: docs/Targets.md. +# +# Never pass CFLAGS_EXTRA= on the command line - it replaces, not appends. +ARCH=AARCH64 +TARGET=s32g2 +SIGN?=ECC384 +HASH?=SHA384 +DEBUG?=1 +DEBUG_UART?=1 +SPMATH?=1 +NO_XIP?=1 +# The HAL sets up the GICv3 itself (hal/s32g2.c); skip the generic GICv2 init. +SKIP_GIC_INIT?=1 +WOLFBOOT_SECTOR_SIZE?=0x1000 +WOLFBOOT_NO_PARTITIONS=1 +GZIP?=1 +DISK_SDCARD?=1 +DISK_FS?=fat32 + +# A/B slots: two files on the third partition, with the boot confirmation +# state in the last 8 bytes of each file. +DISK_BOOT_CONFIRM?=1 +CFLAGS_EXTRA+=-DBOOT_PART_A=2 +CFLAGS_EXTRA+=-DBOOT_PART_B=2 +CFLAGS_EXTRA+=-DBOOT_FILE_A='"/fitImage_a.bin"' +CFLAGS_EXTRA+=-DBOOT_FILE_B='"/fitImage_b.bin"' + +WOLFBOOT_RAMBOOT_MAX_SIZE=0x4000000 +WOLFBOOT_LOAD_ADDRESS?=0xA0000000 +WOLFBOOT_LOAD_DTS_ADDRESS?=0xA5000000 diff --git a/docs/Targets.md b/docs/Targets.md index e3b05e8643..548ddfa2de 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -28,6 +28,7 @@ This README describes configuration of supported targets. * [Nordic nRF54LM20](#nordic-nrf54lm20) * [NXP i.MX 8M Mini](#nxp-imx-8m-mini) * [NXP i.MX 8QuadMax](#nxp-imx-8quadmax) +* [NXP S32G2 (S32G274A GoldBox)](#nxp-s32g2-s32g274a-goldbox) * [NXP i.MX95 Cortex-M7](#nxp-imx95-cortex-m7) * [NXP i.MX95 Cortex-A55 (BL33)](#nxp-imx95-cortex-a55-bl33) * [NXP iMX-RT](#nxp-imx-rt) @@ -10697,6 +10698,153 @@ Register bases used by the HAL, from the upstream device tree (`imx8-ss-{lsio,co `-DDEBUG_SDHCI -DDEBUG_DISK -DDEBUG_GPT` add command-level tracing to the storage paths, and `-DDEBUG_FS` traces the filesystem layer when `DISK_FS` is enabled. +## NXP S32G2 (S32G274A GoldBox) + +wolfBoot runs on the NXP S32G274A (4x Cortex-A53 + 3x Cortex-M7 lock-step pairs) as the Boot ROM's application image, in place of U-Boot: the ROM copies it into the 8 MB system SRAM at 0x340A0000 and enters it at EL3 with only the 48 MHz FIRC running. wolfBoot brings the SoC up, verifies a signed FIT (kernel plus device tree), enters Linux at EL2 and stays resident at EL3 as its PSCI firmware. Developed against the S32G-VNP-GLDBOX, which is an S32G274A-RDB2 in an enclosure. + +``` +Boot ROM -> wolfBoot in SRAM (EL3) -> signed FIT from SD, eMMC or QuadSPI NOR -> Linux (EL2) +``` + +### Example configurations + +| Config | Boot ROM loads wolfBoot from | Signed FIT from | +|---|---|---| +| [/config/examples/s32g2-goldbox.config](/config/examples/s32g2-goldbox.config) | SD card (or eMMC) | FAT32 partition of the SD card; of the eMMC with `DISK_EMMC=1 DISK_SDCARD=0` | +| [/config/examples/s32g2-goldbox-qspi.config](/config/examples/s32g2-goldbox-qspi.config) | QuadSPI NOR | BOOT / UPDATE partitions in the same NOR | + +Both use `SIGN=ECC384 HASH=SHA384`. The disk config has two file slots on the third partition, `/fitImage_a.bin` and `/fitImage_b.bin`: the higher version boots. With wolfBoot's anti-rollback default an older version is not booted when the newer one fails to verify; that needs `ALLOW_DOWNGRADE=1`. The NOR config has BOOT at 1 MB, UPDATE at 17 MB and a SWAP sector at 33 MB, with the FIT read into DDR through the `ext_flash` API. + +Board data is set from the build rather than edited in the HAL, as `-D` definitions in the config's `CFLAGS_EXTRA`: the DRAM bank sizes (`S32G2_DRAM_BANK0_SIZE`, `S32G2_DRAM_BANK1_SIZE`, 2 GB each by default, which must match what the DDR blob trains), the root device on the kernel command line (`LINUX_BOOTARGS_ROOT`, default `/dev/mmcblk0p2`; `LINUX_BOOTARGS` replaces the whole line), and the slot partitions and file names (`BOOT_PART_A`, `BOOT_PART_B`, `BOOT_FILE_A`, `BOOT_FILE_B`). + +The disk config builds with `DISK_BOOT_CONFIRM=1` (see [compile.md](compile.md), "Disk boot confirmation and rollback"). `s32g2-mkfit.sh` pads the signed FIT with 4 KB so the file can carry the 8-byte boot state in its last bytes. Stage and confirm with `lib-fs --dev stage` and `success`; the booted slot's file is named by `/proc/device-tree/chosen/wolfboot,boot-file`. Files copied without staging boot as before and nothing is written to the card. + +### LPDDR4 data blob + +wolfBoot trains the LPDDR4 itself ([/hal/s32g2_ddr.c](/hal/s32g2_ddr.c)) but does not carry the board's controller and PHY register tables or the Synopsys PHY training firmware. NXP publishes both in the BSP U-Boot tree, and [/tools/scripts/s32g2/s32g2-ddr-blob.py](/tools/scripts/s32g2/s32g2-ddr-blob.py) packs them into the binary the build links in: + +``` +git clone -b release/bsp29.0-2020.04 https://github.com/nxp-auto-linux/u-boot.git +tools/scripts/s32g2/s32g2-ddr-blob.py u-boot s32g2_ddr.bin +``` + +`S32G2_DDR_BLOB` names the file (default `s32g2_ddr.bin` in the wolfBoot root). Without it the image still builds, and reports the missing blob on the console instead of training. The blob is checked by magic, version and CRC before use. Inline ECC is not supported. + +### Building + +``` +cp config/examples/s32g2-goldbox.config .config +make +tools/scripts/s32g2/s32g2_mkimage.py wolfboot.bin s32g2_boot.bin +dd if=/dev/sdX bs=512 count=8192 of=sd_raw_backup.bin # keep the stock boot area +dd if=s32g2_boot.bin of=/dev/sdX bs=512 seek=8 conv=fsync # IVT at byte 0x1000 +tools/scripts/s32g2/s32g2-mkfit.sh Image 1 wolfboot_signing_private_key.der fsl-s32g274a-rdb2.dtb +mcopy -i /dev/sdX3 s32g2-fitImage_v1_signed.bin ::/fitImage_a.bin +``` + +[/tools/scripts/s32g2/s32g2_mkimage.py](/tools/scripts/s32g2/s32g2_mkimage.py) writes the IVT (tag 0xD1), a no-op DCD and the application boot code header (tag 0xD5) in the stock BSP's layout. The card must stay MBR: the IVT sits where GPT entries would be. Restoring the backup puts the stock chain back. The stock card's first partition is FAT16, which wolfBoot does not read, so the FIT goes on an added FAT32 partition (`mkfs.vfat -F 32 -s 8`). + +`s32g2-mkfit.sh` builds the FIT from [/hal/s32g2.its](/hal/s32g2.its): the gzip-compressed kernel, loaded at 0x84080000 (2 MB-aligned base plus the image's text offset), and the board DTB, both under the wolfBoot signature. + +### What wolfBoot does before the FIT + +In this order, in [/hal/s32g2.c](/hal/s32g2.c) and the S32G2 block of [/src/boot_aarch64_start.S](/src/boot_aarch64_start.S): + +- SRAM ECC initialisation of `.bss`, the stack and the per-core EL3 stacks through SRAM controller 0 (the ROM only initialises the bytes it copies), then the UART0 pads and LINFlexD_0. +- VR5510 PMIC over I2C4: fail-safe watchdog and FCCU monitoring off, before any clock changes. Then the clock tree: FXOSC, ARM PLL (A53 at 1 GHz), XBAR 800 MHz, PERIPH PLL (LINFlexD 125 MHz, uSDHC 400 MHz, QuadSPI), DDR PLL 800 MHz. +- Ncore coherent interconnect, then an identity MMU at EL3 with the caches on. Nothing is both writable and executable: wolfBoot's code and read-only data are mapped read-only in 4 KB pages, and SRAM data, DRAM and devices are never executable (`SCTLR_EL3.WXN` is set as well). LPDDR4 training (1D and 2D) and a read-back check through a Device mapping follow, then DRAM is mapped Normal. The map stays on after the handoff, with DRAM removed from it, for the PSCI runtime. +- MSCM interrupt routing, GICv3 distributor and redistributors, and the three secondary cores released through MC_ME and parked at EL3. Cluster 1's interconnect port joins only when its cores are released. The uSDHC and QuadSPI pads are routed whichever medium holds the FIT, as the OS expects. + +`hal_dts_fixup` adds the `/memory` node for both DRAM banks, a `/psci` node (`method = "smc"`), each core's `enable-method = "psci"`, the bootargs, and clears `linux,initrd-start/end` (the stock DTB's range overlaps the kernel). The handoff cleans the caches and enters the kernel at EL2 with `SCR_EL3` leaving IRQ and FIQ to EL2. EL3 exception vectors report ESR, ELR and FAR on the console. + +### PSCI + +wolfBoot stays resident at EL3 as the kernel's PSCI 1.1 firmware ([/src/psci.c](/src/psci.c), platform side in [/hal/s32g2.c](/hal/s32g2.c)). An SMC from the kernel is served on a per-core EL3 stack in SRAM. + +| Call | Behaviour | +|---|---| +| `PSCI_VERSION`, `PSCI_FEATURES`, `MIGRATE_INFO_TYPE`, `SMCCC_VERSION` | 1.1, the calls in this table, no Trusted OS, SMCCC 1.1 | +| `CPU_ON` | Wakes the parked core, which enters the kernel at EL2 at the given address. The address must be 4-byte aligned and inside DRAM, and the MPIDR must name one of the four cores | +| `CPU_OFF` | Puts the core's GIC redistributor to sleep and parks the core at EL3, where `CPU_ON` finds it again | +| `AFFINITY_INFO` | On, off or on-pending, for a single core (level 0) | +| `SYSTEM_RESET` | Functional reset through MC_ME: the Boot ROM runs again | +| `SYSTEM_OFF` | Halts the calling core. The PMIC is left on, so the board still draws power | + +Everything else, `CPU_SUSPEND` and the SMCCC architecture workaround calls included, returns `NOT_SUPPORTED`. The code that serves an SMC is built without FP/SIMD register use, since the kernel's state is live in them. + +The SRAM that holds wolfBoot is not isolated from the normal world on this port, so PSCI here is a service to the kernel and not a boundary against it: a kernel that writes the SRAM can change the EL3 code. + +### QuadSPI NOR + +`S32G2_QSPI=1` builds the driver for the QuadSPI controller at 0x40134000 and the GoldBox's 64 MB Macronix MX25UW51245G. The flash runs in plain SPI mode with 4-byte addresses: single commands are IP commands described by a LUT sequence (page program 0x12, 4 KB sector erase 0x21), bulk reads go through the memory-mapped AHB window (fast read 0x0C), the DLL slave is bypassed and data is sampled from the internal DQS loopback. The serial clock comes from PERIPH DFS1 through MC_CGM0 mux 12 (`S32G2_QSPI_2X_DIV`, 8 = 50 MHz). If the Boot ROM left the flash in octal DTR mode, the init resets it to SPI mode with the two-byte OPI reset commands. + +``` +cp config/examples/s32g2-goldbox-qspi.config .config +make +tools/scripts/s32g2/s32g2_mkimage.py wolfboot.bin s32g2_qspi_boot.bin --qspi +``` + +`--qspi` lays the image out for QuadSPI boot: the IVT at flash offset 0, the 0x200-byte QuadSPI parameter block the ROM programs the controller with at 0x200 (`sdr`, the default, reads in plain SPI mode; `--qspi-params octal` is the stock BSP block that moves the flash to octal DTR, which wolfBoot resets back to SPI mode), the DCD and the application. The boot straps are under Hardware setup. + +The NOR is written by a programmer build of the SD config: it reads the named file from the FAT32 partition into DDR, erases, programs and reads it back, then halts. It refuses to erase anything when the flash was not identified, when the partition is not a recognised filesystem, or when the file does not fit. Boot it from the SD card once per image: + +``` +cp config/examples/s32g2-goldbox.config .config +make S32G2_QSPI=1 S32G2_QSPI_PROGRAM_FILE=/s32g2_qspi_boot.bin # wolfBoot at 0 +make S32G2_QSPI=1 S32G2_QSPI_PROGRAM_FILE=/fitImage_a.bin S32G2_QSPI_PROGRAM_OFFSET=0x100000 # FIT in BOOT +``` + +An update is a newer signed FIT in the UPDATE partition plus the update marker, the 8 bytes `FF FF FF 70 'B' 'O' 'O' 'T'` at the end of that partition: + +``` +make S32G2_QSPI=1 S32G2_QSPI_PROGRAM_FILE=/fitImage_v2.bin S32G2_QSPI_PROGRAM_OFFSET=0x1100000 # FIT in UPDATE +make S32G2_QSPI=1 S32G2_QSPI_PROGRAM_FILE=/marker.bin S32G2_QSPI_PROGRAM_OFFSET=0x20FFFF8 # update marker +``` + +The NOR config does not swap partitions: wolfBoot loads the higher version of BOOT and UPDATE into DDR, and records a staged image as `testing` the first time it boots. An image left in `testing` is booted again unless the build allows the older version (`ALLOW_DOWNGRADE=1`). In that case wolfBoot first erases the unconfirmed partition, which takes more than a minute on this flash and prints nothing, and then boots the other one. + +The MX25UW51245G keeps an ECC code for every 16 bytes and allows each such unit to be programmed once per erase. The NOR config therefore keeps the partition state as a journal (`NVM_FLASH_JOURNAL=1`, see [compile.md](compile.md)), one 16-byte record per change, in the last sector of each partition, with 64 KB wolfBoot sectors so that the log of a 16 MB partition fits. Anything else written to this flash has to follow the same rule: erase before programming a 16-byte unit again. + +### eMMC + +The GoldBox routes uSDHC0 either to the microSD slot or to the on-board 32 GB eMMC with the RDB2's SW3 (ON = microSD, OFF = eMMC). `make DISK_EMMC=1 DISK_SDCARD=0` drives the eMMC through the same uSDHC shim. The factory eMMC carries the BSP image (FAT16 boot partition, ext3 rootfs), so a FAT32 partition is appended from the board's Linux (`sfdisk --append`, `mkfs.vfat -F 32 -s 8`) for the FIT files. With wolfBoot in the NOR and SW3 OFF the box boots with no card inserted. + +### Hardware setup (GoldBox) + +- Console: UART0 micro-USB (FTDI FT232R), LINFlexD_0, 115200 8N1. The second micro-USB is UART1 and prints nothing. +- Boot source switches on the RDB2: SD boot is SW4 position 7 ON only; QuadSPI boot is SW4 all OFF; SW9 OFF and SW10 1 ON / 2 OFF for both. +- Expected console, booting from the SD card: + +``` +wolfBoot S32G2 (EL3, running from SRAM) +DDR: 1D training +DDR: 2D training +s32g2: DDR up, 4096 MB, erratum polling 0 +s32g2: core 1 parked +s32g2: core 2 parked +s32g2: core 3 parked +p2: fat32, /fitImage_a.bin +Firmware Valid. +FDT: memory and PSCI set +``` + +### Peripheral map + +| Block | Base | Notes | +|---|---|---| +| LINFlexD_0 | 0x401C8000 | FIFO mode; TX pin PC09 (MSCR41), RX PC10 (MSCR42, IMCR512) | +| uSDHC0 | 0x402F0000 | i.MX uSDHC v3, shared shim [/hal/imx_usdhc_sdhci.c](/hal/imx_usdhc_sdhci.c), per clock 400 MHz | +| QuadSPI | 0x40134000 | AHB window at 0x0 | +| I2C4 | 0x402DC000 | VR5510 fail-safe unit at 0x21 | +| SRAM | 0x34000000 | 8 MB; controllers at 0x4019C000 / 0x401A0000 | +| DRAM | 0x80000000 + 0x880000000 | 2 GB + 2 GB | +| Ncore / GICv3 | 0x50400000 / 0x50800000 | | + +### Known limitations + +- The NOR is not usable from the BSP29 5.4 kernel in this boot chain: updates are staged with the programmer build, and a staged image cannot be confirmed from Linux. +- Inline DRAM ECC, HSE secure boot and chain-loading BL31 (for kernels that need SCMI or PSCI power states) are not implemented. + ## NXP i.MX95 Cortex-M7 The i.MX95 pairs an A55 cluster running Linux with a real-time Cortex-M7 and a Cortex-M33 System Manager. The M7 has no dedicated flash: it is loaded into TCM by the Linux `remoteproc` driver on the A55 side, so wolfBoot is the ELF that `remoteproc` loads, and the images it verifies live in the DDR region the device tree reserves for the M7. diff --git a/hal/imx_usdhc_sdhci.c b/hal/imx_usdhc_sdhci.c index 131e01e0f5..3d822de7f6 100644 --- a/hal/imx_usdhc_sdhci.c +++ b/hal/imx_usdhc_sdhci.c @@ -37,6 +37,8 @@ #include "hal/imx8qm.h" #elif defined(TARGET_imx8mm) #include "hal/imx8mm.h" +#elif defined(TARGET_s32g2) +#include "hal/s32g2.h" #else #error "hal/imx_usdhc_sdhci.c: unsupported target" #endif diff --git a/hal/s32g2.c b/hal/s32g2.c new file mode 100644 index 0000000000..cbad268150 --- /dev/null +++ b/hal/s32g2.c @@ -0,0 +1,2057 @@ +/* s32g2.c + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +/* NXP S32G2 (S32G274A) GoldBox / RDB2: wolfBoot is the Boot ROM's image in + * SRAM at EL3, boots a signed FIT and stays resident as PSCI firmware. See + * docs/Targets.md. */ + +#include +#include +#include +#include + +#if defined(DEBUG_UART) + #define PRINTF_ENABLED +#endif + +#include "image.h" +#include "loader.h" +#include "printf.h" +#include "psci.h" +#include "hal/s32g2.h" +#ifdef S32G2_QSPI_PROGRAM_FILE +#include "disk.h" +#include "disk_fs.h" +#ifndef BOOT_PART_A +#define BOOT_PART_A 2 +#endif +#define S32G2_QSPI_PROGRAM_MAX 0x1000000 /* 16 MB, the NOR partition size */ +#endif +#include "fdt.h" +#if defined(DISK_SDCARD) || defined(DISK_EMMC) +#include "sdhci.h" +#endif + +#ifndef ARCH_AARCH64 +# error "wolfBoot s32g2 HAL: wrong architecture. Compile with ARCH=AARCH64." +#endif + +/* MMIO is Device-nGnRnE, with the MMU off and in the identity map, so + * accesses are strongly ordered by the memory type and need no barriers. */ +static inline uint32_t rd32(uintptr_t a) { return *(volatile uint32_t*)a; } +static inline void wr32(uintptr_t a, uint32_t v) { *(volatile uint32_t*)a = v; } +static inline void wr8(uintptr_t a, uint8_t v) { *(volatile uint8_t*)a = v; } +static inline uint8_t rd8(uintptr_t a) { return *(volatile uint8_t*)a; } +static inline void wr16(uintptr_t a, uint16_t v) { *(volatile uint16_t*)a = v; } +static inline uint64_t rd64(uintptr_t a) { return *(volatile uint64_t*)a; } + +#include "aarch64_arch.h" + +uint64_t hal_get_timer_us(void) +{ + return (timer_get_count() * 1000000ULL) / timer_get_freq(); +} + +void hal_delay_us(uint32_t us) +{ + uint64_t deadline = timer_deadline_us(us); + + while (!timer_expired(deadline)) + ; +} + +/* Clock tree. Out of reset everything runs from the 48 MHz FIRC; this takes + * the A53 cluster, the XBAR, the LIN, SDHC, QuadSPI and DDR clocks to the + * rates the NXP BSP runs them at. */ +/* MC_CGM: clock source select and dividers, one 0x40 block per mux. */ +#define CGM_MUX_CSC(cgm, mux) ((cgm) + 0x300 + (mux) * 0x40) +#define CGM_MUX_CSS(cgm, mux) ((cgm) + 0x304 + (mux) * 0x40) +#define CGM_MUX_DC(cgm, mux, n) ((cgm) + 0x308 + (mux) * 0x40 + (n) * 4) +#define CGM_MUX_DIV_UPD_STAT(cgm, mux) ((cgm) + 0x33C + (mux) * 0x40) +#define CGM_CSC_SELCTL_MASK 0x3F000000 +#define CGM_CSC_SELCTL(src) (((uint32_t)(src) << 24) & CGM_CSC_SELCTL_MASK) +#define CGM_CSC_CLK_SW (1U << 2) +#define CGM_CSS_SELSTAT(css) (((css) >> 24) & 0x3F) +#define CGM_CSS_SWTRG(css) (((css) >> 17) & 0x7) +#define CGM_CSS_SWTRG_SUCCESS 1 +#define CGM_CSS_SWIP (1U << 16) +#define CGM_DC_DE (1U << 31) +#define CGM_DC_DIV(dc) ((((uint32_t)(dc) - 1) << 16) & 0x00FF0000) + +/* Mux source IDs */ +#define CGM_SRC_FIRC 0 +#define CGM_SRC_ARM_PLL_PHI0 4 +#define CGM_SRC_ARM_DFS1 12 +#define CGM_SRC_PERIPH_PLL_PHI3 21 +#define CGM_SRC_PERIPH_DFS1 26 +#define CGM_SRC_PERIPH_DFS3 28 +#define CGM_SRC_DDR_PLL_PHI0 36 + +/* PLLDIG */ +#define PLL_PLLCR 0x00 +#define PLL_PLLSR 0x04 +#define PLL_PLLDV 0x08 +#define PLL_PLLFD 0x10 +#define PLL_PLLCLKMUX 0x20 +#define PLL_PLLODIV(n) (0x80 + (n) * 4) +#define PLL_PLLCR_PLLPD (1U << 31) +#define PLL_PLLSR_LOCK (1U << 2) +#define PLL_PLLDV_RDIV(r) (((uint32_t)(r) & 0x7) << 12) +#define PLL_PLLDV_MFI(m) ((uint32_t)(m) & 0xFF) +#define PLL_PLLFD_SMDEN (1U << 30) +#define PLL_PLLFD_MFN(m) ((uint32_t)(m) & 0x7FFF) +#define PLL_PLLCLKMUX_FXOSC 1 +#define PLL_PLLODIV_DE (1U << 31) +#define PLL_PLLODIV_DIV(dc) ((((uint32_t)(dc) - 1) << 16) & 0x00FF0000) +/* Output dividers per PLL: the register blocks end right after the last one. */ +#define ARM_PLL_NODIVS 2 +#define PERIPH_PLL_NODIVS 8 +#define DDR_PLL_NODIVS 1 + +/* DFS */ +#define DFS_PORTSR 0x0C +#define DFS_PORTOLSR 0x10 +#define DFS_PORTRESET 0x14 +#define DFS_CTL 0x18 +#define DFS_DVPORT(n) (0x1C + (n) * 4) +#define DFS_CTL_RESET (1U << 1) +#define DFS_PORTS_MASK 0x3F +#define DFS_DVPORT_VAL(mfi, mfn) ((((uint32_t)(mfi) & 0xFF) << 8) | ((uint32_t)(mfn) & 0x3F)) + +/* FXOSC */ +#define FXOSC_CTRL 0x00 +#define FXOSC_STAT 0x04 +#define FXOSC_CTRL_OSCON (1U << 0) +#define FXOSC_CTRL_GM_SEL(s) (((uint32_t)(s) & 0xF) << 4) +#define FXOSC_CTRL_EOCV(v) (((uint32_t)(v) & 0xFF) << 16) +#define FXOSC_CTRL_COMP_EN (1U << 24) +#define FXOSC_STAT_OSC_STAT (1U << 31) + +/* MC_ME partition registers */ +#define MC_ME_CTL_KEY 0x000 +#define MC_ME_CTL_KEY_KEY 0x5AF0 +#define MC_ME_CTL_KEY_INVKEY 0xA50F +#define MC_ME_PRTN_PCONF(n) (0x100 + (n) * 0x200) +#define MC_ME_PRTN_PUPD(n) (0x104 + (n) * 0x200) +#define MC_ME_PRTN_STAT(n) (0x108 + (n) * 0x200) +#define MC_ME_PRTN_COFB0_STAT(n) (0x110 + (n) * 0x200) +#define MC_ME_PRTN_COFB0_CLKEN(n) (0x130 + (n) * 0x200) +#define MC_ME_PRTN_PCONF_PCE (1U << 0) +#define MC_ME_PRTN_PUPD_PCUD (1U << 0) +#define MC_ME_PRTN_STAT_PCS (1U << 0) + +/* Bound on hardware polls, so a wedge is reported instead of hanging. */ +#define POLL_MAX 2000000U + +static int poll_set(uintptr_t a, uint32_t mask) +{ + uint32_t n; + + for (n = 0; n < POLL_MAX; n++) { + if ((rd32(a) & mask) != 0) + return 0; + } + return -1; +} + +static int poll_clear(uintptr_t a, uint32_t mask) +{ + uint32_t n; + + for (n = 0; n < POLL_MAX; n++) { + if ((rd32(a) & mask) == 0) + return 0; + } + return -1; +} + +/* Currently selected source of a clock mux */ +uint32_t s32g2_cgm_mux_get(uintptr_t cgm, uint32_t mux) +{ + return CGM_CSS_SELSTAT(rd32(CGM_MUX_CSS(cgm, mux))); +} + +int s32g2_cgm_mux_set(uintptr_t cgm, uint32_t mux, uint32_t src) +{ + uint32_t css, csc; + + css = rd32(CGM_MUX_CSS(cgm, mux)); + if (CGM_CSS_SELSTAT(css) == src && CGM_CSS_SWTRG(css) == CGM_CSS_SWTRG_SUCCESS && + (css & CGM_CSS_SWIP) == 0) + return 0; + if (poll_clear(CGM_MUX_CSS(cgm, mux), CGM_CSS_SWIP) != 0) + return -1; + csc = rd32(CGM_MUX_CSC(cgm, mux)) & ~CGM_CSC_SELCTL_MASK; + csc |= CGM_CSC_SELCTL(src) | CGM_CSC_CLK_SW; + wr32(CGM_MUX_CSC(cgm, mux), csc); + if (poll_clear(CGM_MUX_CSC(cgm, mux), CGM_CSC_CLK_SW) != 0) + return -1; + if (poll_clear(CGM_MUX_CSS(cgm, mux), CGM_CSS_SWIP) != 0) + return -1; + css = rd32(CGM_MUX_CSS(cgm, mux)); + if (CGM_CSS_SWTRG(css) != CGM_CSS_SWTRG_SUCCESS || CGM_CSS_SELSTAT(css) != src) + return -1; + return 0; +} + +static int s32g2_cgm_div_set(uintptr_t cgm, uint32_t mux, uint32_t n, uint32_t dc) +{ + uint32_t val = CGM_DC_DE | CGM_DC_DIV(dc); + + if ((rd32(CGM_MUX_DC(cgm, mux, n)) & (CGM_DC_DE | 0x00FF0000)) == val) + return 0; + wr32(CGM_MUX_DC(cgm, mux, n), val); + return poll_clear(CGM_MUX_DIV_UPD_STAT(cgm, mux), 1); +} + +static int fxosc_enable(void) +{ + uintptr_t b = S32G2_FXOSC_BASE; + uint32_t ctrl; + + /* The Boot ROM may have started it already; its inputs must not change + * while it runs. */ + if ((rd32(b + FXOSC_CTRL) & FXOSC_CTRL_OSCON) != 0) + return 0; + ctrl = FXOSC_CTRL_COMP_EN | FXOSC_CTRL_EOCV(1) | FXOSC_CTRL_GM_SEL(7); + wr32(b + FXOSC_CTRL, ctrl); + wr32(b + FXOSC_CTRL, ctrl | FXOSC_CTRL_OSCON); + return poll_set(b + FXOSC_STAT, FXOSC_STAT_OSC_STAT); +} + +/* VCO = FXOSC * MFI / RDIV with RDIV 1, fractional part off; output dividers + * are disabled around the reprogramming as the PLL is powered down. */ +static int pll_program(uintptr_t pll, uint32_t mfi, uint32_t nodivs) +{ + uint32_t n, dv; + + for (n = 0; n < nodivs; n++) + wr32(pll + PLL_PLLODIV(n), rd32(pll + PLL_PLLODIV(n)) & ~PLL_PLLODIV_DE); + wr32(pll + PLL_PLLCR, PLL_PLLCR_PLLPD); + wr32(pll + PLL_PLLCLKMUX, PLL_PLLCLKMUX_FXOSC); + dv = rd32(pll + PLL_PLLDV) & ~(PLL_PLLDV_RDIV(7) | PLL_PLLDV_MFI(0xFF)); + wr32(pll + PLL_PLLDV, dv | PLL_PLLDV_RDIV(1) | PLL_PLLDV_MFI(mfi)); + wr32(pll + PLL_PLLFD, PLL_PLLFD_SMDEN | PLL_PLLFD_MFN(0)); + wr32(pll + PLL_PLLCR, 0); + return poll_set(pll + PLL_PLLSR, PLL_PLLSR_LOCK); +} + +static void pll_odiv_set(uintptr_t pll, uint32_t n, uint32_t dc) +{ + wr32(pll + PLL_PLLODIV(n), PLL_PLLODIV_DIV(dc)); + wr32(pll + PLL_PLLODIV(n), PLL_PLLODIV_DIV(dc) | PLL_PLLODIV_DE); +} + +/* DFS output = VCO / (2 * (MFI + MFN / 36)). Ports are reset all together + * the first time the DFS is touched, singly afterwards. */ +static int dfs_port_set(uintptr_t dfs, uint32_t port, uint32_t mfi, uint32_t mfn) +{ + uint32_t portsr, mask; + int first; + + portsr = rd32(dfs + DFS_PORTSR); + if ((portsr & (1U << port)) != 0 && + (rd32(dfs + DFS_PORTOLSR) & (1U << port)) == 0 && + rd32(dfs + DFS_DVPORT(port)) == DFS_DVPORT_VAL(mfi, mfn)) + return 0; + first = (portsr == 0); + mask = first ? DFS_PORTS_MASK : (1U << port); + wr32(dfs + DFS_PORTOLSR, mask); + wr32(dfs + DFS_PORTRESET, mask); + if (poll_clear(dfs + DFS_PORTSR, mask) != 0) + return -1; + if (first) + wr32(dfs + DFS_CTL, DFS_CTL_RESET); + wr32(dfs + DFS_DVPORT(port), DFS_DVPORT_VAL(mfi, mfn)); + if (first) + wr32(dfs + DFS_CTL, rd32(dfs + DFS_CTL) & ~DFS_CTL_RESET); + wr32(dfs + DFS_PORTRESET, rd32(dfs + DFS_PORTRESET) & ~(1U << port)); + if (poll_set(dfs + DFS_PORTSR, 1U << port) != 0) + return -1; + if ((rd32(dfs + DFS_PORTOLSR) & (1U << port)) != 0) + return -1; + return 0; +} + +/* A mode entry update takes effect on the key and inverted key. */ +static void mc_me_apply(void) +{ + wr32(S32G2_MC_ME_BASE + MC_ME_CTL_KEY, MC_ME_CTL_KEY_KEY); + wr32(S32G2_MC_ME_BASE + MC_ME_CTL_KEY, MC_ME_CTL_KEY_INVKEY); +} + +/* Clock a block of an MC_ME partition: COFB enable, partition clock enable, + * then the keyed process update. */ +static int s32g2_mc_me_block_enable(uint32_t part, uint32_t block) +{ + uintptr_t me = S32G2_MC_ME_BASE; + + wr32(me + MC_ME_PRTN_COFB0_CLKEN(part), + rd32(me + MC_ME_PRTN_COFB0_CLKEN(part)) | (1U << block)); + wr32(me + MC_ME_PRTN_PCONF(part), + rd32(me + MC_ME_PRTN_PCONF(part)) | MC_ME_PRTN_PCONF_PCE); + wr32(me + MC_ME_PRTN_PUPD(part), + rd32(me + MC_ME_PRTN_PUPD(part)) | MC_ME_PRTN_PUPD_PCUD); + mc_me_apply(); + if (poll_set(me + MC_ME_PRTN_STAT(part), MC_ME_PRTN_STAT_PCS) != 0) + return -1; + return poll_set(me + MC_ME_PRTN_COFB0_STAT(part), 1U << block); +} + +/* A53 at 1 GHz, XBAR_2X 800 MHz, LIN 125 MHz, SDHC 400 MHz, DDR 800 MHz. + * Returns the number of the step that failed, 0 on success. The console + * divisor is reprogrammed here once the LIN clock has moved. */ +static void s32g2_linflex_setup(uint32_t ibrr, uint32_t fbrr); + +static int s32g2_clk_init(void) +{ + /* Park the XBAR on FIRC while its PLL and DFS are reprogrammed. */ + if (s32g2_cgm_mux_set(S32G2_MC_CGM0_BASE, 0, CGM_SRC_FIRC) != 0) + return 1; + if (fxosc_enable() != 0) + return 2; + + /* ARM PLL: 40 MHz * 50 = 2000 MHz VCO, PHI0 = /2 = 1000 MHz -> A53. The + * Boot ROM already runs the cluster from this PLL, so park the cluster + * on FIRC first or powering the PLL down stops the core. */ + if (s32g2_cgm_mux_set(S32G2_MC_CGM1_BASE, 0, CGM_SRC_FIRC) != 0) + return 3; + if (pll_program(S32G2_ARM_PLL_BASE, 50, ARM_PLL_NODIVS) != 0) + return 4; + pll_odiv_set(S32G2_ARM_PLL_BASE, 0, 2); + if (s32g2_cgm_mux_set(S32G2_MC_CGM1_BASE, 0, CGM_SRC_ARM_PLL_PHI0) != 0) + return 5; + + /* ARM DFS port 0 (DFS1): 2000 / (2 * 1.25) = 800 MHz -> XBAR_2X. */ + if (dfs_port_set(S32G2_ARM_DFS_BASE, 0, 1, 9) != 0) + return 6; + if (s32g2_cgm_mux_set(S32G2_MC_CGM0_BASE, 0, CGM_SRC_ARM_DFS1) != 0) + return 7; + + /* PERIPH PLL: 2000 MHz VCO, PHI3 = /16 = 125 MHz -> LIN_BAUD. Same + * precaution for the LIN mux, which the console hangs off. */ + if (s32g2_cgm_mux_set(S32G2_MC_CGM0_BASE, 8, CGM_SRC_FIRC) != 0) + return 8; + if (pll_program(S32G2_PERIPH_PLL_BASE, 50, PERIPH_PLL_NODIVS) != 0) + return 9; + pll_odiv_set(S32G2_PERIPH_PLL_BASE, 3, 16); + if (s32g2_cgm_mux_set(S32G2_MC_CGM0_BASE, 8, CGM_SRC_PERIPH_PLL_PHI3) != 0) + return 10; + s32g2_linflex_setup(S32G2_LINIBRR_115200, S32G2_LINFBRR_115200); + + /* PERIPH DFS port 2 (DFS3): 800 MHz, /2 at the mux -> SDHC 400 MHz. */ + if (dfs_port_set(S32G2_PERIPH_DFS_BASE, 2, 1, 9) != 0) + return 11; + if (s32g2_cgm_div_set(S32G2_MC_CGM0_BASE, 14, 0, 2) != 0) + return 12; + if (s32g2_cgm_mux_set(S32G2_MC_CGM0_BASE, 14, CGM_SRC_PERIPH_DFS3) != 0) + return 13; + if (s32g2_mc_me_block_enable(0, S32G2_MC_ME_USDHC_BLOCK) != 0) + return 14; + + /* DDR PLL: 40 MHz * 40 = 1600 MHz VCO, PHI0 = /2 = 800 MHz -> DDR. */ + if (pll_program(S32G2_DDR_PLL_BASE, 40, DDR_PLL_NODIVS) != 0) + return 15; + pll_odiv_set(S32G2_DDR_PLL_BASE, 0, 2); + if (s32g2_cgm_mux_set(S32G2_MC_CGM5_BASE, 0, CGM_SRC_DDR_PLL_PHI0) != 0) + return 16; + if (s32g2_mc_me_block_enable(0, S32G2_MC_ME_DDR_BLOCK) != 0) + return 17; +#ifdef S32G2_QSPI + /* PERIPH DFS port 0 (DFS1): 800 MHz, divided at the mux to QSPI_2X; + * the serial clock is half of that (/16 = 25 MHz, /8 = 50 MHz). */ + if (dfs_port_set(S32G2_PERIPH_DFS_BASE, 0, 1, 9) != 0) + return 18; + if (s32g2_cgm_div_set(S32G2_MC_CGM0_BASE, 12, 0, S32G2_QSPI_2X_DIV) != 0) + return 19; + if (s32g2_cgm_mux_set(S32G2_MC_CGM0_BASE, 12, CGM_SRC_PERIPH_DFS1) != 0) + return 20; +#endif + return 0; +} + +/* VR5510 PMIC on I2C4: take the fail-safe unit out of its INIT_FS watchdog + * expectations so the board is not reset once the challenger window elapses. + * The I2C controller is the byte-register (IBFD/IBCR/IBSR/IBDR) type, driven + * in polled master mode. */ +#define I2C_IBFD 0x1 +#define I2C_IBCR 0x2 +#define I2C_IBSR 0x3 +#define I2C_IBDR 0x4 +#define IBCR_MDIS (1U << 7) +#define IBCR_MSSL (1U << 5) +#define IBCR_TX (1U << 4) +#define IBCR_NOACK (1U << 3) +#define IBCR_RSTA (1U << 2) +#define IBSR_IBB (1U << 5) +#define IBSR_IBAL (1U << 4) +#define IBSR_IBIF (1U << 1) +#define IBSR_RXAK (1U << 0) + +#define I2C_POLL_MAX 200000U + +/* The PMIC is set up before the clock tree, with the controller clock + * (XBAR / 2 / 3) still at 8 MHz from FIRC: divider 80 gives 100 kHz. */ +#define I2C_IBFD_DIV80 0x14 + +static void i2c_init(uintptr_t b) +{ + wr8(b + I2C_IBFD, I2C_IBFD_DIV80); + wr8(b + I2C_IBCR, IBCR_MDIS); + wr8(b + I2C_IBSR, 0); +} + +static int i2c_wait_idle(uintptr_t b) +{ + uint32_t n; + + for (n = 0; n < I2C_POLL_MAX; n++) { + if ((rd8(b + I2C_IBSR) & IBSR_IBB) == 0) + return 0; + } + return -1; +} + +static int i2c_wait_done(uintptr_t b) +{ + uint32_t n; + uint8_t sr; + + for (n = 0; n < I2C_POLL_MAX; n++) { + sr = rd8(b + I2C_IBSR); + if ((sr & IBSR_IBAL) != 0) { + wr8(b + I2C_IBSR, IBSR_IBAL); + return -1; + } + if ((sr & IBSR_IBIF) != 0) { + wr8(b + I2C_IBSR, IBSR_IBIF); + return 0; + } + } + return -1; +} + +static int i2c_tx_byte(uintptr_t b, uint8_t v) +{ + wr8(b + I2C_IBDR, v); + if (i2c_wait_done(b) != 0) + return -1; + return ((rd8(b + I2C_IBSR) & IBSR_RXAK) != 0) ? -1 : 0; +} + +static int i2c_stop(uintptr_t b) +{ + wr8(b + I2C_IBCR, rd8(b + I2C_IBCR) & ~IBCR_MSSL); + return i2c_wait_idle(b); +} + +/* START, chip address in write mode, register address. */ +static int i2c_start(uintptr_t b, uint8_t chip, uint8_t reg) +{ + wr8(b + I2C_IBCR, IBCR_MDIS); + wr8(b + I2C_IBSR, 0); + wr8(b + I2C_IBCR, 0); + wr8(b + I2C_IBSR, IBSR_IBIF); + if (i2c_wait_idle(b) != 0) + return -1; + wr8(b + I2C_IBCR, rd8(b + I2C_IBCR) | IBCR_MSSL); + wr8(b + I2C_IBCR, rd8(b + I2C_IBCR) | IBCR_TX | IBCR_NOACK); + if (i2c_tx_byte(b, (uint8_t)(chip << 1)) != 0) + return -1; + return i2c_tx_byte(b, reg); +} + +static int i2c_write(uintptr_t b, uint8_t chip, uint8_t reg, + const uint8_t* buf, int len) +{ + int i, ret = 0; + + if (i2c_start(b, chip, reg) != 0) { + i2c_stop(b); + return -1; + } + for (i = 0; i < len; i++) { + ret = i2c_tx_byte(b, buf[i]); + if (ret != 0) + break; + } + i2c_stop(b); + return ret; +} + +static int i2c_read(uintptr_t b, uint8_t chip, uint8_t reg, uint8_t* buf, int len) +{ + int i; + uint8_t cr; + + if (i2c_start(b, chip, reg) != 0) { + i2c_stop(b); + return -1; + } + /* Repeated START, then the chip address in read mode. */ + wr8(b + I2C_IBCR, rd8(b + I2C_IBCR) | IBCR_RSTA); + if (i2c_tx_byte(b, (uint8_t)((chip << 1) | 1)) != 0) { + i2c_stop(b); + return -1; + } + cr = rd8(b + I2C_IBCR) & ~IBCR_TX; + if (len == 1) + cr |= IBCR_NOACK; + else + cr &= ~IBCR_NOACK; + wr8(b + I2C_IBCR, cr); + (void)rd8(b + I2C_IBDR); /* dummy read starts the receive */ + for (i = 0; i < len; i++) { + if (i2c_wait_done(b) != 0) { + i2c_stop(b); + return -1; + } + if (i == len - 2) + wr8(b + I2C_IBCR, rd8(b + I2C_IBCR) | IBCR_NOACK); + else if (i == len - 1) + wr8(b + I2C_IBCR, rd8(b + I2C_IBCR) & ~IBCR_MSSL); + buf[i] = rd8(b + I2C_IBDR); + } + i2c_stop(b); + return 0; +} + +/* VR5510 register access: 16-bit registers, big-endian on the wire, followed + * by a CRC-8 (poly 0x1D, seed 0xFF) over {chip<<1|rw, reg, data_hi, data_lo}. */ +#define VR5510_CRC_POLY 0x1D +#define VR5510_CRC_SEED 0xFF + +static uint8_t vr5510_crc(const uint8_t* d, int len) +{ + uint8_t crc = VR5510_CRC_SEED; + int i, bit; + + for (i = 0; i < len; i++) { + crc ^= d[i]; + for (bit = 0; bit < 8; bit++) { + crc = (uint8_t)((crc & 0x80) ? ((crc << 1) ^ VR5510_CRC_POLY) : (crc << 1)); + } + } + return crc; +} + +static int vr5510_read(uint8_t reg, uint16_t* val) +{ + const uint8_t chip = S32G2_VR5510_FSU_ADDR; + uint8_t frame[5]; + + if (i2c_read(S32G2_I2C4_BASE, chip, reg, &frame[2], 3) != 0) + return -1; + frame[0] = (uint8_t)((chip << 1) | 1); + frame[1] = reg; + if (vr5510_crc(frame, 4) != frame[4]) + return -2; + *val = (uint16_t)(((uint16_t)frame[2] << 8) | frame[3]); + return 0; +} + +static int vr5510_write(uint8_t reg, uint16_t val) +{ + const uint8_t chip = S32G2_VR5510_FSU_ADDR; + uint8_t frame[5]; + + frame[0] = (uint8_t)(chip << 1); + frame[1] = reg; + frame[2] = (uint8_t)(val >> 8); + frame[3] = (uint8_t)val; + frame[4] = vr5510_crc(frame, 4); + return i2c_write(S32G2_I2C4_BASE, chip, reg, &frame[2], 3); +} + +/* Fail-safe unit registers */ +#define VR5510_FS_I_WD_CFG 7 +#define VR5510_FS_I_SAFE_INPUTS 9 +#define VR5510_FS_I_NOT_SAFE_INPUTS 10 +#define VR5510_FS_WD_WINDOW 15 +#define VR5510_FS_NOT_WD_WINDOW 16 +#define VR5510_FS_WD_SEED 17 +#define VR5510_FS_WD_ANSWER 18 +#define VR5510_FS_DIAG_SAFETY 22 +#define VR5510_FS_STATES 24 +#define VR5510_STATE_MASK 0x1F +#define VR5510_STATE_INIT_FS 6 +#define VR5510_WD_WINDOW_MASK 0xF000 +#define VR5510_FCCU_CFG_MASK (3U << 14) +#define VR5510_ABIST1_OK (1U << 10) +#define VR5510_ERR_CNT_MASK 0xF + +/* Answer the challenger watchdog once: the seed is transformed as the + * datasheet prescribes and the error counter must read zero afterwards. */ +static int vr5510_wd_refresh(void) +{ + uint16_t reg; + uint32_t answer; + + if (vr5510_read(VR5510_FS_WD_SEED, ®) != 0) + return -1; + answer = ((~((uint32_t)reg * 4 + 2)) / 4) & 0xFFFFU; + if (vr5510_write(VR5510_FS_WD_ANSWER, (uint16_t)answer) != 0) + return -2; + if (vr5510_read(VR5510_FS_I_WD_CFG, ®) != 0) + return -3; + return ((reg & VR5510_ERR_CNT_MASK) != 0) ? -4 : 0; +} + +static int s32g2_pmic_init(void) +{ + uint16_t reg; + int ret; + + /* I2C4 pads (PC01 SDA, PC02 SCL): open drain, both directions. */ + wr32(S32G2_SIUL2_0_MSCR_BASE + 4 * S32G2_I2C4_SDA_MSCR, S32G2_I2C4_SDA_MSCR_VAL); + wr32(S32G2_SIUL2_0_MSCR_BASE + 4 * S32G2_I2C4_SCL_MSCR, S32G2_I2C4_SCL_MSCR_VAL); + wr32(S32G2_SIUL2_1_IMCR_BASE + 4 * (S32G2_I2C4_SDA_IMCR - S32G2_SIUL2_1_IMCR_START), + S32G2_I2C4_IMCR_VAL); + wr32(S32G2_SIUL2_1_IMCR_BASE + 4 * (S32G2_I2C4_SCL_IMCR - S32G2_SIUL2_1_IMCR_START), + S32G2_I2C4_IMCR_VAL); + i2c_init(S32G2_I2C4_BASE); + + ret = vr5510_read(VR5510_FS_STATES, ®); + if (ret != 0) { + wolfBoot_printf("s32g2: VR5510 FS_STATES read failed (%d)\n", ret); + return -1; + } + if ((reg & VR5510_STATE_MASK) != VR5510_STATE_INIT_FS) + return 0; /* an earlier stage already took it out of INIT_FS */ + + /* Watchdog window off, written with its complement as the FSU requires. */ + if (vr5510_read(VR5510_FS_WD_WINDOW, ®) != 0) + return -2; + reg &= (uint16_t)~VR5510_WD_WINDOW_MASK; + if (vr5510_write(VR5510_FS_WD_WINDOW, reg) != 0 || + vr5510_write(VR5510_FS_NOT_WD_WINDOW, (uint16_t)~reg) != 0) + return -3; + + if (vr5510_read(VR5510_FS_DIAG_SAFETY, ®) != 0) + return -4; + if ((reg & VR5510_ABIST1_OK) == 0) { + wolfBoot_printf("s32g2: VR5510 ABIST1 not OK (0x%04x)\n", reg); + return -5; + } + + /* FCCU monitoring off, same complement write. */ + if (vr5510_read(VR5510_FS_I_SAFE_INPUTS, ®) != 0) + return -6; + reg &= (uint16_t)~VR5510_FCCU_CFG_MASK; + if (vr5510_write(VR5510_FS_I_SAFE_INPUTS, reg) != 0 || + vr5510_write(VR5510_FS_I_NOT_SAFE_INPUTS, (uint16_t)~reg) != 0) + return -7; + + ret = vr5510_wd_refresh(); + if (ret != 0) { + wolfBoot_printf("s32g2: VR5510 watchdog refresh failed (%d)\n", ret); + return -8; + } + return 0; +} + +/* What the OS expects an EL3 firmware to have done for a four-core A53 + * system: the Ncore interconnect and the MSCM interrupt router, the GICv3 + * secure-side setup on each core, and the three secondary cores released + * and parked at EL3 until the kernel turns them on through PSCI. */ + +/* Ncore coherent interconnect: directory snoop filters online, then the + * cluster's CAIU admitted to snooping and DVM. */ +#define NCORE_CSR (S32G2_NCORE_BASE + 0xFF000) +#define NCORE_CSADSER (NCORE_CSR + 0x40) +#define NCORE_CSUIDR (NCORE_CSR + 0xFF8) +#define NCORE_CSIDR (NCORE_CSR + 0xFFC) +#define NCORE_DIRU(n) (S32G2_NCORE_BASE + 0x80000 + (n) * 0x1000) +#define NCORE_DIRUSFER(n) (NCORE_DIRU(n) + 0x10) +#define NCORE_DIRUCASER(n) (NCORE_DIRU(n) + 0x40) +#define NCORE_DIRUSFMCR(n) (NCORE_DIRU(n) + 0x80) +#define NCORE_DIRUSFMAR(n) (NCORE_DIRU(n) + 0x84) +#define NCORE_CAIUIDR(n) (S32G2_NCORE_BASE + (n) * 0x1000 + 0xFFC) +#define CSUIDR_NUMDIRUS(v) (((v) >> 16) & 0x3F) +#define CSIDR_NUMSFS(v) (((v) >> 18) & 0x1F) +#define CAIUIDR_TYPE_MASK (0xFU << 16) +#define CAIUIDR_TYPE_ACE_DVM (0x0U << 16) + +static void ncore_caiu_online(uint32_t caiu) +{ + uint32_t numdirus = CSUIDR_NUMDIRUS(rd32(NCORE_CSUIDR)); + uint32_t diru; + + for (diru = 0; diru < numdirus; diru++) + wr32(NCORE_DIRUCASER(diru), rd32(NCORE_DIRUCASER(diru)) | (1U << caiu)); + if ((rd32(NCORE_CAIUIDR(caiu)) & CAIUIDR_TYPE_MASK) == CAIUIDR_TYPE_ACE_DVM) + wr32(NCORE_CSADSER, rd32(NCORE_CSADSER) | (1U << caiu)); +} + +static int s32g2_ncore_init(void) +{ + uint32_t numdirus = CSUIDR_NUMDIRUS(rd32(NCORE_CSUIDR)); + uint32_t numsfs = CSIDR_NUMSFS(rd32(NCORE_CSIDR)) + 1; + uint32_t diru, sf; + + /* The ROM isolated cluster 0 from the interconnect; drop that first. */ + wr32(S32G2_NCORE_CAIUTC, rd32(S32G2_NCORE_CAIUTC) & ~S32G2_NCORE_CAIUTC_ISOLEN); + for (diru = 0; diru < numdirus; diru++) { + for (sf = 0; sf < numsfs; sf++) { + wr32(NCORE_DIRUSFMCR(diru), sf << 16); /* initialise all entries */ + if (poll_clear(NCORE_DIRUSFMAR(diru), 1U) != 0) + return -1; + wr32(NCORE_DIRUSFER(diru), rd32(NCORE_DIRUSFER(diru)) | (1U << sf)); + } + } + /* Cluster 1's port joins when its cores are released: a port admitted + * to DVM with its cores in reset never answers a DVM sync, and the DSB + * after a TLB invalidate then hangs. */ + ncore_caiu_online(0); + return 0; +} + +/* MSCM interrupt router: every shared peripheral interrupt to all four cores. */ +static void s32g2_mscm_init(void) +{ + uint32_t i; + + for (i = 0; i < S32G2_MSCM_IRSPRC_NUM; i++) + wr16(S32G2_MSCM_BASE + S32G2_MSCM_IRSPRC + i * 2, 0xF); +} + +/* GICv3 distributor: affinity routing on, every SPI in Group 1 non-secure. */ +static void s32g2_gic_init_dist(void) +{ + uintptr_t d = S32G2_GICD_BASE; + uint32_t lines = rd32(d + GICD_TYPER) & 0x1F; + uint32_t i; + + wr32(d + GICD_CTLR, GICD_CTLR_ARE_NS | GICD_CTLR_ARE_S | GICD_CTLR_ENGRP1S | + GICD_CTLR_ENGRP1NS | GICD_CTLR_ENGRP0); + for (i = 1; i <= lines; i++) { + wr32(d + GICD_IGROUPR + i * 4, 0xFFFFFFFFU); + wr32(d + GICD_IGROUPMODR + i * 4, 0); + } +} + +/* GICv3 redistributor and CPU interface of the calling core, at EL3. */ +static uintptr_t s32g2_gicr_self(void) +{ + uintptr_t r = S32G2_GICR_BASE; + uint64_t mpidr, aff; + uint32_t n; + + __asm__ volatile("mrs %0, mpidr_el1" : "=r"(mpidr)); + aff = ((mpidr >> 32) & 0xFF) << 24 | (mpidr & 0x00FFFFFF); + for (n = 0; n < 8; n++, r += S32G2_GICR_STRIDE) { + if ((rd64(r + GICR_TYPER) >> 32) == aff) + return r; + } + return 0; +} + +static void s32g2_gic_init_cpu(void) +{ + uintptr_t r = s32g2_gicr_self(); + + if (r == 0) + return; + wr32(r + GICR_WAKER, rd32(r + GICR_WAKER) & ~GICR_WAKER_PROCESSORSLEEP); + __asm__ volatile("dsb st; isb"); + if (poll_clear(r + GICR_WAKER, GICR_WAKER_CHILDRENASLEEP) != 0) { + /* The SGI and PPI frame must not be touched while it sleeps. */ + wolfBoot_printf("s32g2: GIC redistributor did not wake\n"); + wolfBoot_panic(); + } + wr32(r + GICR_SGI_BASE + GICR_IGROUPR0, 0xFFFFFFFFU); + wr32(r + GICR_SGI_BASE + GICR_IGROUPMODR0, 0); + wr32(r + GICR_SGI_BASE + GICR_ISENABLER0, 1); + + __asm__ volatile( + "mrs x0, icc_sre_el3\n" + "orr x0, x0, #0xf\n" + "msr icc_sre_el3, x0\n" + "isb\n" + "mov x0, #0x3\n" + "msr icc_igrpen1_el3, x0\n" + "isb\n" + "msr icc_ctlr_el3, xzr\n" + "isb\n" + "mrs x0, icc_sre_el2\n" + "orr x0, x0, #0xf\n" + "msr icc_sre_el2, x0\n" + "isb\n" + "msr icc_ctlr_el1, xzr\n" + "isb\n" + "mov x0, #0x80\n" + "msr icc_pmr_el1, x0\n" + "isb\n" + ::: "x0", "memory"); +} + +/* The calling core is going off: no interrupt groups, redistributor asleep. + * s32g2_gic_init_cpu() undoes it when the core is turned on again. */ +static void s32g2_gic_sleep_cpu(void) +{ + uintptr_t r = s32g2_gicr_self(); + + __asm__ volatile( + "msr icc_igrpen1_el3, xzr\n" + "msr icc_igrpen0_el1, xzr\n" + "isb\n"); + if (r == 0) + return; + wr32(r + GICR_WAKER, rd32(r + GICR_WAKER) | GICR_WAKER_PROCESSORSLEEP); + (void)poll_set(r + GICR_WAKER, GICR_WAKER_CHILDRENASLEEP); +} + +static void s32g2_el3_mmu_on(void); + +/* Called by each secondary core from the startup code, on its own SRAM + * stack, before it parks at EL3 (s32g2_psci_wait). */ +void s32g2_secondary_main(void); +void s32g2_secondary_main(void) +{ + __asm__ volatile("msr cntfrq_el0, %0" :: "r"((uint64_t)S32G2_CNTFRQ_HZ)); + s32g2_gic_init_cpu(); + /* The PSCI state is shared between the cores and locked with exclusive + * accesses, which need the caches. */ + s32g2_el3_mmu_on(); +} + +/* PSCI platform side (src/psci.c). The SMC vector and the parking loop's + * frame are in src/boot_aarch64_start.S. */ +#if WOLFBOOT_PSCI_NUM_CPUS != S32G2_NUM_CORES +#error "WOLFBOOT_PSCI_NUM_CPUS must match the S32G2 core count" +#endif + +struct s32g2_psci_start { + uint64_t entry; + uint64_t context; +}; + +void s32g2_psci_park(void) __attribute__((noreturn)); + +/* A parked core waits here, at EL3 on its own stack, until CPU_ON names it; + * the startup code then enters the kernel at EL2. */ +struct s32g2_psci_start s32g2_psci_wait(uint32_t id); +struct s32g2_psci_start s32g2_psci_wait(uint32_t id) +{ + struct s32g2_psci_start start; + + start.entry = 0; + start.context = 0; + while (!wolfBoot_psci_cpu_take(id, &start.entry, &start.context)) + __asm__ volatile("wfe"); + s32g2_gic_init_cpu(); + return start; +} + +int hal_psci_mpidr_to_cpu(uint64_t mpidr) +{ + /* two clusters of two cores: aff1 and aff0 are each 0 or 1 */ + if ((mpidr & ~0x101ULL) != 0) + return -1; + return (int)(((mpidr >> 8) & 1) * 2 + (mpidr & 1)); +} + +unsigned int hal_psci_this_cpu(void) +{ + uint64_t mpidr; + + __asm__ volatile("mrs %0, mpidr_el1" : "=r"(mpidr)); + return (unsigned int)(((mpidr >> 8) & 0xFF) * 2 + (mpidr & 0x3)); +} + +int hal_psci_entry_ok(uint64_t entry) +{ + if ((entry & 3) != 0) + return 0; + if (entry >= S32G2_DRAM_BASE && + entry - S32G2_DRAM_BASE < S32G2_DRAM_BANK0_SIZE) + return 1; + return (entry >= S32G2_DRAM_BANK1_BASE && + entry - S32G2_DRAM_BANK1_BASE < S32G2_DRAM_BANK1_SIZE); +} + +void hal_psci_cpu_wake(unsigned int cpu) +{ + /* every parked core wakes and looks at its own state */ + (void)cpu; + __asm__ volatile("dsb sy; sev"); +} + +void hal_psci_cpu_off(void) +{ + s32g2_gic_sleep_cpu(); + /* The kernel's timer event stream would wake the parked core thousands + * of times a second; the kernel sets it up again on the way back. */ + __asm__ volatile( + "mrs x0, cnthctl_el2\n" + "bic x0, x0, #(1 << 2)\n" + "msr cnthctl_el2, x0\n" + "mrs x0, cntkctl_el1\n" + "bic x0, x0, #(1 << 2)\n" + "msr cntkctl_el1, x0\n" + "isb\n" + ::: "x0"); + s32g2_psci_park(); +} + +/* Functional reset of the SoC through the mode entry module: the Boot ROM + * runs again and reloads wolfBoot. */ +#define MC_ME_MODE_CONF 0x004 +#define MC_ME_MODE_UPD 0x008 +#define MC_ME_MODE_CONF_FUNC_RST (1U << 1) +#define MC_ME_MODE_UPD_UPD (1U << 0) +void hal_psci_system_reset(void) +{ + wr32(S32G2_MC_ME_BASE + MC_ME_MODE_CONF, MC_ME_MODE_CONF_FUNC_RST); + wr32(S32G2_MC_ME_BASE + MC_ME_MODE_UPD, MC_ME_MODE_UPD_UPD); + mc_me_apply(); + for (;;) + __asm__ volatile("wfi"); +} + +/* The PMIC is left on: the calling core stops here, and the kernel has + * already stopped the others. */ +void hal_psci_system_off(void) +{ + for (;;) + __asm__ volatile("wfi"); +} + +/* MC_ME core enable and MC_RGM reset release for the secondaries. The core + * start address is this image's entry; the startup code sends secondaries + * to s32g2_secondary_entry. Cluster 1 (cores 2, 3) first needs its CAIU. */ +#define MC_ME_PRTN_CORE(n, m) (S32G2_MC_ME_BASE + 0x140 + (n) * 0x200 + (m) * 0x20) +#define MC_ME_CORE_PCONF 0x0 +#define MC_ME_CORE_PUPD 0x4 +#define MC_ME_CORE_STAT 0x8 +#define MC_ME_CORE_ADDR 0xC +#define MC_ME_CORE_CCE 1U +#define MC_ME_CORE_CCUPD 1U +#define MC_ME_CORE_CCS 1U +#define MC_ME_A53_PART 1 +#define MC_RGM_PRST(g) (S32G2_MC_RGM_BASE + 0x40 + (g) * 8) +#define MC_RGM_PSTAT(g) (S32G2_MC_RGM_BASE + 0x140 + (g) * 8) +#define MC_RGM_A53_GROUP 1 + +static int s32g2_core_release(uint32_t core, uintptr_t entry) +{ + uintptr_t cl = MC_ME_PRTN_CORE(MC_ME_A53_PART, core & ~1U); + uint32_t bit = 1U << (core + 1); + + wr32(MC_ME_PRTN_CORE(MC_ME_A53_PART, core) + MC_ME_CORE_ADDR, + (uint32_t)entry & 0xFFFFFFFCU); + /* Clock enable is per cluster, defined on its first core's registers. */ + if ((rd32(cl + MC_ME_CORE_STAT) & MC_ME_CORE_CCS) == 0) { + wr32(cl + MC_ME_CORE_PCONF, MC_ME_CORE_CCE); + wr32(cl + MC_ME_CORE_PUPD, MC_ME_CORE_CCUPD); + mc_me_apply(); + if (poll_set(cl + MC_ME_CORE_STAT, MC_ME_CORE_CCS) != 0) + return -1; + } + wr32(MC_RGM_PRST(MC_RGM_A53_GROUP), rd32(MC_RGM_PRST(MC_RGM_A53_GROUP)) & ~bit); + return (poll_clear(MC_RGM_PSTAT(MC_RGM_A53_GROUP), bit) != 0) ? -2 : 0; +} + +static int s32g2_smp_release(uintptr_t entry) +{ + uint32_t core, n; + int ret; + + wolfBoot_psci_init(0); + + wr32(S32G2_MC_ME_BASE + MC_ME_PRTN_PCONF(MC_ME_A53_PART), 1U); + wr32(S32G2_MC_ME_BASE + MC_ME_PRTN_PUPD(MC_ME_A53_PART), 1U); + mc_me_apply(); + + for (core = 1; core < S32G2_NUM_CORES; core++) { + if (core == 2) + ncore_caiu_online(1); + ret = s32g2_core_release(core, entry); + if (ret != 0) { + wolfBoot_printf("s32g2: core %u release failed (%d)\n", core, ret); + return ret; + } + for (n = 0; n < POLL_MAX; n++) { + if (wolfBoot_psci_cpu_alive(core)) + break; + } + wolfBoot_printf("s32g2: core %u %s\n", core, + (n == POLL_MAX) ? "did not check in" : "parked"); + } + return 0; +} + +/* QuadSPI controller driving the GoldBox's 64 MB Macronix MX25UW51245G NOR + * in plain SPI mode (one data line, SDR, 4-byte addresses). Single commands + * are IP commands described by a LUT sequence; bulk reads go through the + * memory-mapped AHB window. */ +#ifdef S32G2_QSPI + +#define QSPI_MCR 0x000 +#define QSPI_MCR_SWRSTSD (1U << 0) +#define QSPI_MCR_SWRSTHD (1U << 1) +#define QSPI_MCR_END_CFG (3U << 2) +#define QSPI_MCR_DQS_EN (1U << 6) +#define QSPI_MCR_CLR_RXF (1U << 10) +#define QSPI_MCR_CLR_TXF (1U << 11) +#define QSPI_MCR_MDIS (1U << 14) +#define QSPI_MCR_RESERVED (0xFU << 16) +#define QSPI_MCR_DQS_INT_PAD (1U << 24) +#define QSPI_IPCR 0x008 +#define QSPI_IPCR_SEQID(s) ((uint32_t)(s) << 24) +#define QSPI_FLSHCR 0x00C +#define QSPI_FLSHCR_TCSS(n) ((uint32_t)(n) << 0) +#define QSPI_FLSHCR_TCSH(n) ((uint32_t)(n) << 8) +#define QSPI_BUF3CR 0x01C +#define QSPI_BUF3CR_ALLMST (1U << 31) +#define QSPI_BUF3CR_ADATSZ(n) ((uint32_t)(n) << 8) +#define QSPI_BFGENCR 0x020 +#define QSPI_BFGENCR_SEQID(s) ((uint32_t)(s) << 12) +#define QSPI_BUF0IND 0x030 +#define QSPI_BUF1IND 0x034 +#define QSPI_BUF2IND 0x038 +#define QSPI_DLLCRA 0x060 +#define QSPI_DLLCR_SLV_UPD_EN (1U << 0) +#define QSPI_DLLCR_SLV_BYPASS_EN (1U << 1) +#define QSPI_DLLCR_SLV_EN (1U << 2) +#define QSPI_SFAR 0x100 +#define QSPI_SFACR 0x104 +#define QSPI_SMPR 0x108 +#define QSPI_SMPR_FSPHS (1U << 5) +#define QSPI_RBCT 0x110 +#define QSPI_RBCT_WMRK_MASK 0x1FU +#define QSPI_RBCT_RXBRD_USEIPS (1U << 8) +#define QSPI_DLLSR 0x12C +#define QSPI_DLLSR_SLVA_LOCK (1U << 14) +#define QSPI_TBSR 0x150 +#define QSPI_TBSR_TRCTR(v) ((v) >> 16) +#define QSPI_TBSR_TRBFL(v) ((v) & 0xFFU) +#define QSPI_TBDR 0x154 +#define QSPI_SR 0x15C +#define QSPI_SR_BUSY (1U << 0) +#define QSPI_SR_IP_ACC (1U << 1) +#define QSPI_SR_AHB_ACC (1U << 2) +#define QSPI_SPTRCLR 0x16C +#define QSPI_SPTRCLR_BFPTRC (1U << 0) +#define QSPI_SPTRCLR_IPPTRC (1U << 8) +#define QSPI_SFA1AD 0x180 +#define QSPI_SFA2AD 0x184 +#define QSPI_SFB1AD 0x188 +#define QSPI_SFB2AD 0x18C +#define QSPI_RBDR(n) (0x200 + 4 * (n)) +#define QSPI_LUTKEY 0x300 +#define QSPI_LUTKEY_VALUE 0x5AF05AF0U +#define QSPI_LCKCR 0x304 +#define QSPI_LCKCR_LOCK 1U +#define QSPI_LCKCR_UNLOCK 2U +#define QSPI_LUT_BASE 0x310 +#define QSPI_LUT_WORDS 5 /* words per sequence on S32 */ +#define QSPI_LUT(seq, i) (QSPI_LUT_BASE + ((seq) * QSPI_LUT_WORDS + (i)) * 4) +#define QSPI_SEQ_IP 15 +#define QSPI_SEQ_AHB 14 + +#define LUT_STOP 0 +#define LUT_CMD 1 +#define LUT_ADDR 2 +#define LUT_DUMMY 3 +#define LUT_READ 7 +#define LUT_WRITE 8 +#define LUT_CMD_DDR 17 +/* instruction[15:10] | pad[9:8] (0 = one line, 3 = eight) | operand[7:0] */ +#define LUT_ENTRY(ins, opr) ((uint16_t)(((ins) << 10) | ((opr) & 0xFF))) +#define LUT_ENTRY_OCTAL(ins, opr) ((uint16_t)(((ins) << 10) | (3 << 8) | ((opr) & 0xFF))) +#define QSPI_MCR_DDR_EN (1U << 7) +#define QSPI_FLSHCR_TDH(n) ((uint32_t)(n) << 16) + +/* The RX buffer holds 128 bytes; one IP command moves at most that much. */ +#define QSPI_XFER_MAX 128U + +#define FLASH_ID_MX25UW51245G { 0xC2, 0x81, 0x3A } +#define FLASH_CMD_RSTEN 0x66 +#define FLASH_CMD_RST 0x99 +#define FLASH_CMD_RDID 0x9F +#define FLASH_CMD_RDSR 0x05 +#define FLASH_CMD_WREN 0x06 +#define FLASH_CMD_READ4B 0x0C /* fast read, 8 dummy cycles */ +#define FLASH_CMD_PP4B 0x12 +#define FLASH_CMD_SE4B 0x21 /* 4 KB sector */ +#define FLASH_SR_WIP 0x01 +#define FLASH_PAGE_SIZE 256U +#define FLASH_SECTOR_SIZE 4096U + +#define FLASH_WIP_POLL_MAX 200000U /* sector erase takes up to ~400 ms */ + +static void qspi_ahb_setup(void); + +static int qspi_wait_idle(void) +{ + return poll_clear(S32G2_QSPI_BASE + QSPI_SR, + QSPI_SR_BUSY | QSPI_SR_IP_ACC | QSPI_SR_AHB_ACC); +} + +static void qspi_lut_program(uint32_t seq, const uint16_t* e, uint32_t n) +{ + uintptr_t b = S32G2_QSPI_BASE; + uint32_t i, w; + + wr32(b + QSPI_LUTKEY, QSPI_LUTKEY_VALUE); + wr32(b + QSPI_LCKCR, QSPI_LCKCR_UNLOCK); + for (i = 0; i < QSPI_LUT_WORDS; i++) { + w = 0; + if (2 * i < n) + w |= e[2 * i]; + if (2 * i + 1 < n) + w |= (uint32_t)e[2 * i + 1] << 16; + wr32(b + QSPI_LUT(seq, i), w); + } + wr32(b + QSPI_LUTKEY, QSPI_LUTKEY_VALUE); + wr32(b + QSPI_LCKCR, QSPI_LCKCR_LOCK); +} + +/* One IP command: opcode, optional 32-bit address, dummy cycles, then up to + * QSPI_XFER_MAX bytes out of tx or into rx. */ +static int qspi_op(uint8_t cmd, int has_addr, uint32_t addr, uint8_t dummy, + const uint8_t* tx, uint8_t* rx, uint32_t len) +{ + uintptr_t b = S32G2_QSPI_BASE; + uint16_t e[6]; + uint32_t n = 0, i, words, v, spin; + + if (len > QSPI_XFER_MAX) + return -1; + e[n++] = LUT_ENTRY(LUT_CMD, cmd); + if (has_addr) + e[n++] = LUT_ENTRY(LUT_ADDR, 0x20); /* 32 address bits */ + if (dummy != 0) + e[n++] = LUT_ENTRY(LUT_DUMMY, dummy); + if (len != 0) + e[n++] = LUT_ENTRY(rx != NULL ? LUT_READ : LUT_WRITE, len); + e[n++] = LUT_ENTRY(LUT_STOP, 0); + + if (qspi_wait_idle() != 0) + return -1; + wr32(b + QSPI_SFAR, addr); + wr32(b + QSPI_MCR, rd32(b + QSPI_MCR) | QSPI_MCR_CLR_RXF | QSPI_MCR_CLR_TXF); + wr32(b + QSPI_SPTRCLR, QSPI_SPTRCLR_BFPTRC | QSPI_SPTRCLR_IPPTRC); + qspi_lut_program(QSPI_SEQ_IP, e, n); + wr32(b + QSPI_RBCT, QSPI_RBCT_WMRK_MASK | QSPI_RBCT_RXBRD_USEIPS); + + words = (len + 3) / 4; + if (tx != NULL && len != 0) { + if (poll_clear(b + QSPI_TBSR, QSPI_TBSR_TRBFL(~0U)) != 0) + return -1; + for (i = 0; i < words; i++) { + v = 0; + memcpy(&v, tx + 4 * i, (len - 4 * i < 4) ? (len - 4 * i) : 4); + wr32(b + QSPI_TBDR, v); + } + } + wr32(b + QSPI_IPCR, len | QSPI_IPCR_SEQID(QSPI_SEQ_IP)); + if (tx != NULL && len != 0) { + for (spin = 0; spin < POLL_MAX; spin++) { + v = rd32(b + QSPI_TBSR); + if (QSPI_TBSR_TRCTR(v) == words && QSPI_TBSR_TRBFL(v) == 0) + break; + } + if (spin == POLL_MAX) + return -1; + } + if (qspi_wait_idle() != 0) + return -1; + if (rx != NULL && len != 0) { + for (i = 0; i < words; i++) { + v = rd32(b + QSPI_RBDR(i)); + memcpy(rx + 4 * i, &v, (len - 4 * i < 4) ? (len - 4 * i) : 4); + } + } + return 0; +} + +/* Reset both domains so a later AHB access does not see stale buffers. */ +static void qspi_invalidate(void) +{ + uintptr_t b = S32G2_QSPI_BASE; + uint32_t mcr = rd32(b + QSPI_MCR) & ~QSPI_MCR_MDIS; + + wr32(b + QSPI_MCR, mcr); + wr32(b + QSPI_MCR, mcr | QSPI_MCR_SWRSTHD | QSPI_MCR_SWRSTSD); + wr32(b + QSPI_MCR, mcr | QSPI_MCR_SWRSTHD | QSPI_MCR_SWRSTSD | QSPI_MCR_MDIS); + wr32(b + QSPI_MCR, mcr | QSPI_MCR_MDIS); + wr32(b + QSPI_MCR, mcr); +} + +/* The DLL slave in bypass mode, as the SDR configuration needs it. */ +static int qspi_dll_bypass(void) +{ + uintptr_t b = S32G2_QSPI_BASE; + uint32_t v = QSPI_DLLCR_SLV_EN | QSPI_DLLCR_SLV_BYPASS_EN; + int ret; + + wr32(b + QSPI_DLLCRA, v); + wr32(b + QSPI_DLLCRA, v | QSPI_DLLCR_SLV_UPD_EN); + ret = poll_set(b + QSPI_DLLSR, QSPI_DLLSR_SLVA_LOCK); + wr32(b + QSPI_DLLCRA, v); + return ret; +} + +/* A Boot ROM that loaded this image from the NOR with the stock parameters + * left the flash in octal DTR mode (volatile CR2). Software reset through + * two 8-line DDR commands puts it back into SPI mode. */ +static int qspi_octal_reset(void) +{ + uintptr_t b = S32G2_QSPI_BASE; + uint32_t mcr = rd32(b + QSPI_MCR); + uint16_t e[3]; + volatile uint32_t spin; + int ret; + + wr32(b + QSPI_FLSHCR, QSPI_FLSHCR_TCSS(3) | QSPI_FLSHCR_TCSH(3) | QSPI_FLSHCR_TDH(1)); + wr32(b + QSPI_MCR, mcr | QSPI_MCR_DDR_EN); + /* Macronix OPI commands are the opcode followed by its complement. */ + e[0] = LUT_ENTRY_OCTAL(LUT_CMD_DDR, FLASH_CMD_RSTEN); + e[1] = LUT_ENTRY_OCTAL(LUT_CMD_DDR, (uint8_t)~FLASH_CMD_RSTEN); + e[2] = LUT_ENTRY(LUT_STOP, 0); + ret = qspi_wait_idle(); + if (ret == 0) { + wr32(b + QSPI_SFAR, 0); + wr32(b + QSPI_SPTRCLR, QSPI_SPTRCLR_BFPTRC | QSPI_SPTRCLR_IPPTRC); + qspi_lut_program(QSPI_SEQ_IP, e, 3); + wr32(b + QSPI_IPCR, QSPI_IPCR_SEQID(QSPI_SEQ_IP)); + ret = qspi_wait_idle(); + } + if (ret == 0) { + e[0] = LUT_ENTRY_OCTAL(LUT_CMD_DDR, FLASH_CMD_RST); + e[1] = LUT_ENTRY_OCTAL(LUT_CMD_DDR, (uint8_t)~FLASH_CMD_RST); + wr32(b + QSPI_SPTRCLR, QSPI_SPTRCLR_BFPTRC | QSPI_SPTRCLR_IPPTRC); + qspi_lut_program(QSPI_SEQ_IP, e, 3); + wr32(b + QSPI_IPCR, QSPI_IPCR_SEQID(QSPI_SEQ_IP)); + ret = qspi_wait_idle(); + } + wr32(b + QSPI_MCR, mcr); + wr32(b + QSPI_FLSHCR, QSPI_FLSHCR_TCSS(3) | QSPI_FLSHCR_TCSH(3)); + /* tREADY after a software reset is well under a millisecond. */ + for (spin = 0; spin < 2000000U; spin++) + ; + qspi_invalidate(); + return ret; +} + +static int s32g2_qspi_init(void) +{ + uintptr_t b = S32G2_QSPI_BASE; + volatile uint32_t spin; + static const uint8_t expect[3] = FLASH_ID_MX25UW51245G; + uint8_t id[3] = { 0, 0, 0 }; + int ret; + + wr32(b + QSPI_MCR, QSPI_MCR_SWRSTSD | QSPI_MCR_SWRSTHD); + for (spin = 0; spin < 1000; spin++) + ; + wr32(b + QSPI_MCR, QSPI_MCR_MDIS | QSPI_MCR_RESERVED); + /* SDR timing: no data hold, three cycles of chip-select setup/hold. */ + wr32(b + QSPI_FLSHCR, QSPI_FLSHCR_TCSS(3) | QSPI_FLSHCR_TCSH(3)); + wr32(b + QSPI_SMPR, QSPI_SMPR_FSPHS); + wr32(b + QSPI_BUF0IND, 0); + wr32(b + QSPI_BUF1IND, 0); + wr32(b + QSPI_BUF2IND, 0); + wr32(b + QSPI_BFGENCR, QSPI_BFGENCR_SEQID(QSPI_SEQ_IP)); + wr32(b + QSPI_RBCT, QSPI_RBCT_WMRK_MASK); + wr32(b + QSPI_BUF3CR, QSPI_BUF3CR_ALLMST | QSPI_BUF3CR_ADATSZ(1024 / 8)); + /* One device on bus A, chip select 0. */ + wr32(b + QSPI_SFA1AD, S32G2_QSPI_FLASH_SIZE); + wr32(b + QSPI_SFA2AD, S32G2_QSPI_FLASH_SIZE); + wr32(b + QSPI_SFB1AD, S32G2_QSPI_FLASH_SIZE); + wr32(b + QSPI_SFB2AD, S32G2_QSPI_FLASH_SIZE); + wr32(b + QSPI_SFACR, 0); + wr32(b + QSPI_MCR, QSPI_MCR_RESERVED | QSPI_MCR_END_CFG | + QSPI_MCR_DQS_INT_PAD | QSPI_MCR_DQS_EN); + ret = qspi_dll_bypass(); + if (ret != 0) + return ret; + qspi_ahb_setup(); + /* The flash answers RDID only in SPI mode; otherwise it was left in + * octal DTR by the Boot ROM, so reset it and ask again. */ + if (qspi_op(FLASH_CMD_RDID, 0, 0, 0, NULL, id, 3) == 0 && memcmp(id, expect, 3) == 0) + return 0; + wolfBoot_printf("qspi: id %02x %02x %02x, resetting the flash out of octal mode\n", + id[0], id[1], id[2]); + if (qspi_octal_reset() != 0) + return -2; + if (qspi_op(FLASH_CMD_RDID, 0, 0, 0, NULL, id, 3) != 0 || + memcmp(id, expect, 3) != 0) + return -3; + return 0; +} + +/* Memory-mapped reads through the AHB window: the controller fetches with + * the sequence in BFGENCR into its 1 KB buffer. Only whole words, as the + * window is Device memory while the MMU is off. */ +static void qspi_ahb_setup(void) +{ + uint16_t e[5]; + + e[0] = LUT_ENTRY(LUT_CMD, FLASH_CMD_READ4B); + e[1] = LUT_ENTRY(LUT_ADDR, 0x20); + e[2] = LUT_ENTRY(LUT_DUMMY, 8); + e[3] = LUT_ENTRY(LUT_READ, QSPI_XFER_MAX); + e[4] = LUT_ENTRY(LUT_STOP, 0); + qspi_lut_program(QSPI_SEQ_AHB, e, 5); + wr32(S32G2_QSPI_BASE + QSPI_BFGENCR, QSPI_BFGENCR_SEQID(QSPI_SEQ_AHB)); +} + +static int qspi_ahb_read(uint32_t addr, uint8_t* buf, uint32_t len) +{ + uintptr_t win = (uintptr_t)S32G2_QSPI_AHB_BASE + addr; + const volatile uint64_t* src; + uint64_t* dst = (uint64_t*)buf; + uint32_t i, n = len / 8; + + /* The window starts at address 0. Hide the value from the compiler, + * which may otherwise treat a read through a null pointer as + * unreachable. */ + __asm__ volatile("" : "+r"(win)); + src = (const volatile uint64_t*)win; + if (qspi_wait_idle() != 0) + return -1; + for (i = 0; i < n; i++) + dst[i] = src[i]; + return 0; +} + +static int s32g2_qspi_read(uint32_t addr, uint8_t* buf, uint32_t len) +{ + uint32_t n; + + /* Bulk through the window when source and destination are 8-byte + * aligned; the IP path takes the remaining tail, or the whole read + * otherwise. */ + if (len >= 1024 && (addr & 7) == 0 && (((uintptr_t)buf) & 7) == 0) { + n = len & ~7U; + if (qspi_ahb_read(addr, buf, n) != 0) + return -1; + addr += n; + buf += n; + len -= n; + } + while (len > 0) { + n = (len > QSPI_XFER_MAX) ? QSPI_XFER_MAX : len; + if (qspi_op(FLASH_CMD_READ4B, 1, addr, 8, NULL, buf, n) != 0) + return -1; + addr += n; + buf += n; + len -= n; + } + return 0; +} + +static int flash_wait_ready(void) +{ + uint8_t sr; + uint32_t spin; + + for (spin = 0; spin < FLASH_WIP_POLL_MAX; spin++) { + if (qspi_op(FLASH_CMD_RDSR, 0, 0, 0, NULL, &sr, 1) != 0) + return -1; + if ((sr & FLASH_SR_WIP) == 0) + return 0; + } + return -1; +} + +static int flash_write_enable(void) +{ + return qspi_op(FLASH_CMD_WREN, 0, 0, 0, NULL, NULL, 0); +} + +static int s32g2_qspi_write(uint32_t addr, const uint8_t* buf, uint32_t len) +{ + uint32_t n, page_left; + + while (len > 0) { + page_left = FLASH_PAGE_SIZE - (addr & (FLASH_PAGE_SIZE - 1)); + n = (len > page_left) ? page_left : len; + if (n > QSPI_XFER_MAX) + n = QSPI_XFER_MAX; + if (flash_write_enable() != 0 || + qspi_op(FLASH_CMD_PP4B, 1, addr, 0, buf, NULL, n) != 0 || + flash_wait_ready() != 0) + return -1; + addr += n; + buf += n; + len -= n; + } + qspi_invalidate(); + return 0; +} + +/* Erase whole 4 KB sectors covering [addr, addr + len). */ +static int s32g2_qspi_erase(uint32_t addr, uint32_t len) +{ + uint32_t end = addr + len; + + if (len == 0) + return 0; + addr &= ~(FLASH_SECTOR_SIZE - 1); + while (addr < end) { + if (flash_write_enable() != 0 || + qspi_op(FLASH_CMD_SE4B, 1, addr, 0, NULL, NULL, 0) != 0 || + flash_wait_ready() != 0) + return -1; + addr += FLASH_SECTOR_SIZE; + } + qspi_invalidate(); + return 0; +} + +#ifdef EXT_FLASH +/* A length from an image header is not trusted yet: keep every access + * inside the device. */ +static int qspi_range_ok(uintptr_t address, int len) +{ + if (len < 0 || (uint64_t)address > (uint64_t)S32G2_QSPI_FLASH_SIZE || + (uint64_t)len > (uint64_t)S32G2_QSPI_FLASH_SIZE - + (uint64_t)address) + return 0; + return 1; +} + +int ext_flash_read(uintptr_t address, uint8_t *data, int len) +{ + if (!qspi_range_ok(address, len) || + s32g2_qspi_read((uint32_t)address, data, (uint32_t)len) != 0) + return -1; + return len; +} + +int ext_flash_write(uintptr_t address, const uint8_t *data, int len) +{ + if (!qspi_range_ok(address, len) || + s32g2_qspi_write((uint32_t)address, data, (uint32_t)len) != 0) + return -1; + return 0; +} + +int ext_flash_erase(uintptr_t address, int len) +{ + if (!qspi_range_ok(address, len)) + return -1; + return s32g2_qspi_erase((uint32_t)address, (uint32_t)len); +} + +void ext_flash_lock(void) +{ +} + +void ext_flash_unlock(void) +{ +} +#endif /* EXT_FLASH */ + + +#endif /* S32G2_QSPI */ + +/* Console LINFlexD_0. The pads and the UART are set up whether or not + * wolfBoot prints: the kernel's console driver takes both as it finds them. */ + +#define LINFLEX_INIT_POLL_MAX 1000000U +/* Program LINFlexD_0 for 8N1 FIFO mode with the given divisor. */ +static void s32g2_linflex_setup(uint32_t ibrr, uint32_t fbrr) +{ + uintptr_t b = S32G2_LINFLEX0_BASE; + uint32_t n; + + /* Enter init mode (INIT, then MME|INIT) and wait for LINS to report it. */ + wr32(b + LINFLEX_LINCR1, LINFLEX_LINCR1_INIT); + wr32(b + LINFLEX_LINCR1, LINFLEX_LINCR1_MME | LINFLEX_LINCR1_INIT); + for (n = 0; n < LINFLEX_INIT_POLL_MAX; n++) { + if ((rd32(b + LINFLEX_LINSR) & LINFLEX_LINSR_LINS_MASK) == + LINFLEX_LINSR_LINS_INIT) + break; + } + /* UART mode must be selected before the other UARTCR fields are written. */ + wr32(b + LINFLEX_UARTCR, LINFLEX_UARTCR_UART); + wr32(b + LINFLEX_LINIBRR, ibrr); + wr32(b + LINFLEX_LINFBRR, fbrr); + wr32(b + LINFLEX_UARTPTO, 0xF); + wr32(b + LINFLEX_UARTCR, LINFLEX_UARTCR_8N1_FIFO); + wr32(b + LINFLEX_LINCR1, LINFLEX_LINCR1_MME); +} + +static void s32g2_console_init(void) +{ + /* The Boot ROM leaves the pads unrouted and the LIN clock on FIRC. */ + wr32(S32G2_SIUL2_0_MSCR_BASE + 4 * S32G2_UART0_TX_MSCR, + S32G2_UART0_TX_MSCR_VAL); + wr32(S32G2_SIUL2_0_MSCR_BASE + 4 * S32G2_UART0_RX_MSCR, + S32G2_UART0_RX_MSCR_VAL); + wr32(S32G2_SIUL2_0_IMCR_BASE + 4 * (S32G2_UART0_RX_IMCR - 512), + S32G2_UART0_RX_IMCR_VAL); + s32g2_linflex_setup(S32G2_LINIBRR_115200_FIRC, S32G2_LINFBRR_115200_FIRC); +} + +#if defined(DEBUG_UART) +static void linflex_putc(char c) +{ + uintptr_t b = S32G2_LINFLEX0_BASE; + /* The generic timer is not guaranteed to count until FXOSC is up, so bound + * the wait with a plain spin instead of a timer deadline. */ + uint32_t spin = 0; + + while ((rd32(b + LINFLEX_UARTSR) & LINFLEX_UARTSR_DTF) != 0) { + if (++spin > 1000000U) + return; + } + /* Byte-wide write: in FIFO mode the access size is the byte count sent. */ + wr8(b + LINFLEX_BDRL, (uint8_t)c); +} + +void uart_write(const char* buf, unsigned int sz) +{ + unsigned int i; + + for (i = 0; i < sz; i++) { + if (buf[i] == '\n') + linflex_putc('\r'); + linflex_putc(buf[i]); + } +} +#endif /* DEBUG_UART */ + +/* The device tree always comes from the signed FIT. */ +void* hal_get_dts_address(void) +{ + return NULL; +} + +void* hal_get_dts_update_address(void) +{ + return NULL; +} + +#if defined(DEBUG) && defined(DEBUG_UART) +/* EL3 exception report, called from s32g2_el3_fault_common + * (src/boot_aarch64_start.S); the vector halts in wfi after this. */ + +extern uint8_t _start_text[]; +extern uint8_t _end[]; + +void s32g2_el3_fault_handler(unsigned long esr, unsigned long elr, + unsigned long far, unsigned long vector); +void s32g2_el3_fault_handler(unsigned long esr, unsigned long elr, + unsigned long far, unsigned long vector) +{ + wolfBoot_printf("\n*** S32G2 EL3 EXCEPTION ***\n"); + wolfBoot_printf("vector=%d ESR=0x%08x EC=0x%02x ISS=0x%06x\n", (int)vector, + (uint32_t)esr, (uint32_t)((esr >> 26) & 0x3F), (uint32_t)(esr & 0x1FFFFFFUL)); + wolfBoot_printf("ELR=0x%08x%08x FAR=0x%08x%08x\n", + (uint32_t)(elr >> 32), (uint32_t)(elr & 0xFFFFFFFFUL), + (uint32_t)(far >> 32), (uint32_t)(far & 0xFFFFFFFFUL)); + if (elr >= (uintptr_t)_start_text && elr < (uintptr_t)_end) { + wolfBoot_printf("ELR is inside wolfBoot (+0x%x)\n", + (uint32_t)(elr - (uintptr_t)_start_text)); + } +} +#endif /* DEBUG && DEBUG_UART */ + +#if defined(MMU) && defined(WOLFBOOT_FDT) + +/* Console LINFlexD_0 (ttyLF0); root is the stock card's second partition. */ +#ifndef LINUX_BOOTARGS +#ifndef LINUX_BOOTARGS_ROOT +#define LINUX_BOOTARGS_ROOT "/dev/mmcblk0p2" +#endif +#define LINUX_BOOTARGS \ + "console=ttyLF0,115200 earlycon root=" LINUX_BOOTARGS_ROOT \ + " rootwait rw" +#endif + +/* Describe the two DDR banks and the PSCI firmware, and make PSCI each cpu + * node's enable-method. */ +static int s32g2_dts_fixup_cpus_memory(fdt_ctx* ctx) +{ + static const char psci_compat[] = "arm,psci-1.0\0arm,psci-0.2"; + uint64_t reg[4]; + int off, ret; + + /* First: a node added here moves the offsets of everything after it. */ + off = fdt_subnode_offset(ctx, 0, "psci"); + if (off == -FDT_ERR_NOTFOUND) + off = fdt_add_subnode(ctx, 0, "psci"); + if (off < 0) + return off; + ret = fdt_setprop(ctx, off, "compatible", psci_compat, + (int)sizeof(psci_compat)); + if (ret == 0) + ret = fdt_setprop(ctx, off, "method", "smc", 4); + if (ret != 0) + return ret; + + off = fdt_find_devtype(ctx, 0, "memory"); + if (off < 0) { + off = fdt_add_subnode(ctx, 0, "memory@80000000"); + if (off < 0) + return off; + ret = fdt_setprop(ctx, off, "device_type", "memory", 7); + if (ret != 0) + return ret; + } + reg[0] = cpu_to_fdt64((uint64_t)S32G2_DRAM_BASE); + reg[1] = cpu_to_fdt64((uint64_t)S32G2_DRAM_BANK0_SIZE); + reg[2] = cpu_to_fdt64((uint64_t)S32G2_DRAM_BANK1_BASE); + reg[3] = cpu_to_fdt64((uint64_t)S32G2_DRAM_BANK1_SIZE); + ret = fdt_setprop(ctx, off, "reg", reg, (int)sizeof(reg)); + if (ret != 0) + return ret; + + off = fdt_path_offset(ctx, "/cpus"); + if (off < 0) + return off; + off = fdt_find_devtype(ctx, off, "cpu"); + while (off >= 0) { + ret = fdt_setprop(ctx, off, "enable-method", "psci", 5); + if (ret != 0) + return ret; + off = fdt_find_devtype(ctx, off, "cpu"); + } + wolfBoot_printf("FDT: memory and PSCI set\n"); + return 0; +} + +static int s32g2_dts_fixup(void* dts_addr, uint32_t capacity) +{ + fdt_ctx ctx; + int off; + int ret; + + ret = fdt_open(&ctx, dts_addr, capacity); + if (ret == 0) + ret = fdt_grow(&ctx, WOLFBOOT_FDT_FIXUP_HEADROOM); + /* Before the /chosen edits, since inserting nodes moves the offsets + * after them. */ + if (ret == 0) + ret = s32g2_dts_fixup_cpus_memory(&ctx); + if (ret != 0) + return ret; + /* ERR050543: with derating left off, the kernel's DDR driver has to + * poll MR4, and it only probes an enabled node. */ + if (s32g2_ddr_polling_needed()) { + off = fdt_node_offset_by_compatible(&ctx, -1, "fsl,s32gen1-ddr"); + if (off < 0) + wolfBoot_printf("FDT: no DDR node for erratum polling (%d)\n", + off); + else + ret = fdt_fixup_str(&ctx, off, "ddr", "status", "okay"); + } + if (ret == 0) + ret = fdt_fixup_bootargs(&ctx, LINUX_BOOTARGS, 1); + /* Zero the stock DTB's initrd range unless the FIT supplied one: it is + * unverified memory and overlaps the kernel load address. */ +#ifdef WOLFBOOT_FIT_RAMDISK + if (ret == 0 && !fit_ramdisk_loaded()) +#else + if (ret == 0) +#endif + ret = fdt_fixup_initrd(&ctx, 0, 0); +#ifdef WOLFBOOT_UPDATE_DISK + /* Tell the OS which slot it booted from, as the default fixup does. */ + if (ret == 0) + ret = wolfBoot_disk_dts_fixup(&ctx); +#endif + return ret; +} + +int hal_dts_fixup(void* dts_addr, uint32_t capacity) +{ + int ret; + + if (dts_addr == NULL) + return -1; + ret = s32g2_dts_fixup(dts_addr, capacity); + if (ret != 0) { + /* Never hand the kernel a tree left half fixed, from any boot + * source. */ + wolfBoot_printf("s32g2: device tree fixup failed (%d)\n", ret); + wolfBoot_panic(); + } + return ret; +} +#endif /* MMU && WOLFBOOT_FDT */ + +static int s32g2_ddr_test(uintptr_t base, uint32_t len) +{ + volatile uint64_t* p = (volatile uint64_t*)base; + uint32_t n = len / sizeof(uint64_t); + uint32_t i; + uint64_t v; + + for (i = 0; i < n; i++) + p[i] = ((uint64_t)(base + i * 8) * 0x9E3779B97F4A7C15ULL) ^ (uint64_t)i; + for (i = 0; i < n; i++) { + v = ((uint64_t)(base + i * 8) * 0x9E3779B97F4A7C15ULL) ^ (uint64_t)i; + if (p[i] != v) { + wolfBoot_printf("s32g2: DDR mismatch at 0x%08x%08x: 0x%08x%08x != 0x%08x%08x\n", + (uint32_t)((uint64_t)(uintptr_t)&p[i] >> 32), (uint32_t)(uintptr_t)&p[i], + (uint32_t)(p[i] >> 32), (uint32_t)p[i], (uint32_t)(v >> 32), (uint32_t)v); + return -1; + } + } + return 0; +} + +/* Let the 4-entry TX FIFO drain before the LIN clock under it changes. Run + * with the MMU off, where one pass costs several microseconds: at least + * 15 ms. */ +static void linflex_drain(void) +{ + volatile uint32_t spin; + + for (spin = 0; spin < 4000U; spin++) + ; +} + + +/* uSDHC0 pads: CLK, CMD/DATA0-7/VSELECT with pull-ups, RST, and the input + * muxes; the clock and MC_ME block come up with the clock tree. Routed + * whether or not wolfBoot reads the card itself, as the OS expects the boot + * firmware to have done it. */ +static void s32g2_sd_pads(void) +{ + uint32_t v; + + wr32(S32G2_SIUL2_0_MSCR_BASE + 4 * 46, S32G2_SD_CLK_MSCR_VAL); + for (v = 47; v <= 55; v++) + wr32(S32G2_SIUL2_0_MSCR_BASE + 4 * v, S32G2_SD_DATA_MSCR_VAL); + wr32(S32G2_SIUL2_0_MSCR_BASE + 4 * 56, S32G2_SD_RST_MSCR_VAL); + for (v = 515; v <= 524; v++) + wr32(S32G2_SIUL2_0_IMCR_BASE + 4 * (v - 512), S32G2_SD_IMCR_VAL); +} + +/* EL3 identity map, 4 KB granule, 36-bit VA. SRAM and trained DRAM are + * Normal write-back, everything else Device: with the MMU off all data is + * Device and the hash and inflate take tens of seconds. Only wolfBoot's text + * is executable, and it is read-only. The map stays on after the handoff for + * the PSCI runtime, with DRAM removed from it. */ +#define EL3_XN (1ULL << 54) +#define EL3_RO (1ULL << 7) +#define EL3_BLOCK_NORMAL (0x0000000000000701ULL | EL3_XN) +#define EL3_BLOCK_DEVICE (0x0000000000000405ULL | EL3_XN | (1ULL << 53)) +#define EL3_PAGE_DATA (0x0000000000000703ULL | EL3_XN) +#define EL3_PAGE_TEXT (0x0000000000000703ULL | EL3_RO) +#define EL3_TABLE 0x3ULL +#define EL3_SCTLR_WXN (1ULL << 19) +static uint64_t s32g2_el3_l1[64] __attribute__((aligned(4096))); +static uint64_t s32g2_el3_l2_gb0[512] __attribute__((aligned(4096))); +static uint64_t s32g2_el3_l3_img[512] __attribute__((aligned(4096))); + +/* Page-aligned bounds of the image's code and read-only data (hal/s32g2.ld) */ +extern uint8_t _start_text[]; +extern uint8_t _end_text[]; + +static void s32g2_el3_mmu_enable(void) +{ + uint64_t a; + uint64_t text = (uint64_t)(uintptr_t)_start_text; + uint64_t text_end = (uint64_t)(uintptr_t)_end_text; + uint64_t img = text & ~0x1FFFFFULL; + uint32_t i; + + for (i = 0; i < 512; i++) { + a = (uint64_t)i << 21; + s32g2_el3_l2_gb0[i] = a | ((a >= S32G2_SRAM_BASE && + a < S32G2_SRAM_BASE + S32G2_SRAM_SIZE) ? EL3_BLOCK_NORMAL : EL3_BLOCK_DEVICE); + } + for (i = 0; i < 512; i++) { + a = img + ((uint64_t)i << 12); + s32g2_el3_l3_img[i] = a | + ((a >= text && a < text_end) ? EL3_PAGE_TEXT : EL3_PAGE_DATA); + } + s32g2_el3_l2_gb0[img >> 21] = + (uint64_t)(uintptr_t)s32g2_el3_l3_img | EL3_TABLE; + for (i = 0; i < 64; i++) + s32g2_el3_l1[i] = 0; + s32g2_el3_l1[0] = (uint64_t)(uintptr_t)s32g2_el3_l2_gb0 | EL3_TABLE; + s32g2_el3_l1[1] = (1ULL << 30) | EL3_BLOCK_DEVICE; + /* DRAM stays unmapped until it is trained (s32g2_el3_mmu_map_dram), so + * nothing can speculate into it. */ + __asm__ volatile("dsb sy"); + + aarch64_dcache_maint(0); + s32g2_el3_mmu_on(); +} + +/* Per core: point it at the tables and turn the MMU and caches on. */ +static void s32g2_el3_mmu_on(void) +{ + uint64_t sctlr; + + /* Attr0 Normal WB/WA, Attr1 Device-nGnRnE; T0SZ 28, inner-shareable + * cacheable walks, 40-bit PA, bits 23 and 31 RES1. */ + __asm__ volatile("msr mair_el3, %0" :: "r"(0x00000000000000FFULL)); + __asm__ volatile("msr ttbr0_el3, %0" :: "r"((uint64_t)(uintptr_t)s32g2_el3_l1)); + __asm__ volatile("msr tcr_el3, %0" :: "r"(0x000000008082351CULL)); + __asm__ volatile("isb"); + __asm__ volatile("tlbi alle3"); + __asm__ volatile("dsb sy"); + __asm__ volatile("isb"); + __asm__ volatile("ic iallu"); + __asm__ volatile("dsb sy"); + __asm__ volatile("isb"); + __asm__ volatile("mrs %0, sctlr_el3" : "=r"(sctlr)); + sctlr |= (1ULL << 0) | (1ULL << 2) | (1ULL << 12) | EL3_SCTLR_WXN; + __asm__ volatile("msr sctlr_el3, %0" :: "r"(sctlr)); + __asm__ volatile("isb"); +} + +/* Both DRAM banks with the given block attributes: Device for the uncached + * read-back check after training, Normal write-back once it has passed, and + * 0 to take DRAM out of the map again, which the handoff + * (src/boot_aarch64_start.S) does before it cleans the caches. */ +void s32g2_el3_mmu_map_dram(uint64_t attr); +void s32g2_el3_mmu_map_dram(uint64_t attr) +{ + uint64_t i; + int pass; + + /* Break before make: the entries are first made invalid and flushed + * from the TLB, and only then written with the new attributes. Nothing + * touches DRAM in between; this code and its stack are in SRAM. */ + for (pass = 0; pass < 2; pass++) { + for (i = (uint64_t)S32G2_DRAM_BASE >> 30; i < 64 && (i << 30) < + (uint64_t)S32G2_DRAM_BASE + S32G2_DRAM_BANK0_SIZE; i++) + s32g2_el3_l1[i] = (pass == 0 || attr == 0) ? 0 : ((i << 30) | attr); + for (i = S32G2_DRAM_BANK1_BASE >> 30; i < 64 && (i << 30) < + S32G2_DRAM_BANK1_BASE + S32G2_DRAM_BANK1_SIZE; i++) + s32g2_el3_l1[i] = (pass == 0 || attr == 0) ? 0 : ((i << 30) | attr); + __asm__ volatile("dsb sy"); + __asm__ volatile("tlbi alle3is"); + __asm__ volatile("dsb sy"); + __asm__ volatile("isb"); + } +} + +/* LPDDR4 training, then a read-back check through a Device mapping so a + * failed training cannot pass on cached data; only then is DRAM cacheable. */ +static void s32g2_dram_init(void) +{ + int ret; + + ret = s32g2_ddr_init(); + if (ret != 0) { + wolfBoot_printf("s32g2: DDR init failed (%d)\n", ret); + wolfBoot_panic(); + } + s32g2_el3_mmu_map_dram(EL3_BLOCK_DEVICE); + if (s32g2_ddr_test((uintptr_t)S32G2_DRAM_BASE, 0x400000) != 0 || + s32g2_ddr_test((uintptr_t)WOLFBOOT_LOAD_ADDRESS, 0x400000) != 0 || + s32g2_ddr_test((uintptr_t)S32G2_DRAM_BANK1_BASE, 0x400000) != 0) { + wolfBoot_printf("s32g2: DDR read-back test FAILED\n"); + wolfBoot_panic(); + } + s32g2_el3_mmu_map_dram(EL3_BLOCK_NORMAL); + wolfBoot_printf("s32g2: DDR up, %u MB, erratum polling %d\n", + (unsigned)(S32G2_DRAM_SIZE >> 20), s32g2_ddr_polling_needed()); +} + +#ifdef S32G2_QSPI +static void s32g2_qspi_pads(void) +{ + uint32_t v; + + for (v = 85; v <= 93; v++) + wr32(S32G2_SIUL2_0_MSCR_BASE + 4 * v, S32G2_QSPI_DATA_MSCR_VAL); + for (v = 96; v <= 99; v++) + wr32(S32G2_SIUL2_0_MSCR_BASE + 4 * v, S32G2_QSPI_CLK_MSCR_VAL); + wr32(S32G2_SIUL2_0_MSCR_BASE + 4 * 100, S32G2_QSPI_CS_MSCR_VAL); + wr32(S32G2_SIUL2_0_MSCR_BASE + 4 * 101, S32G2_QSPI_CS_MSCR_VAL); + for (v = 540; v <= 548; v++) + wr32(S32G2_SIUL2_0_IMCR_BASE + 4 * (v - 512), S32G2_QSPI_IMCR_VAL); +} +#endif + +#ifdef S32G2_QSPI_PROGRAM_FILE +/* NOR programmer build: read an image file from the FAT partition of the SD + * card or eMMC into DDR, erase and program it at S32G2_QSPI_PROGRAM_OFFSET, + * read it back, then halt. */ +static void s32g2_nor_program(int qspi_ret) +{ + static struct fs_volume vol; + static struct fs_file f; + uint8_t* img = (uint8_t*)(uintptr_t)WOLFBOOT_LOAD_ADDRESS; + uint8_t* chk = img + S32G2_QSPI_PROGRAM_MAX; + uint32_t n = 0, off, len; + int ret; + + /* Nothing is erased unless the flash was identified. */ + ret = qspi_ret; + if (ret == 0) + ret = disk_init(0); + if (ret >= 0) + ret = disk_open(0); + if (ret >= 0) + ret = fs_mount(&vol, 0, BOOT_PART_A); + /* An unrecognised filesystem mounts as a raw volume whose "file" is the + * whole partition, whatever the name: only a real file is programmed. */ + if (ret == 0 && vol.type == FS_TYPE_RAW) + ret = -3; + if (ret == 0) + ret = fs_open(&vol, &f, S32G2_QSPI_PROGRAM_FILE, + S32G2_QSPI_PROGRAM_MAX); + if (ret == 0 && (f.size == 0 || f.size > S32G2_QSPI_PROGRAM_MAX || + (uint64_t)S32G2_QSPI_PROGRAM_OFFSET >= + (uint64_t)S32G2_QSPI_FLASH_SIZE || + f.size > (uint64_t)S32G2_QSPI_FLASH_SIZE - + (uint64_t)S32G2_QSPI_PROGRAM_OFFSET)) + ret = -4; + if (ret == 0) { + for (off = 0; off < (uint32_t)f.size; off += 0x10000) { + len = (uint32_t)f.size - off; + if (len > 0x10000) + len = 0x10000; + if (fs_read(&f, off, len, img + off) != (int)len) { + ret = -1; + break; + } + } + if (ret == 0) + n = (uint32_t)f.size; + } + wolfBoot_printf("s32g2: NOR image %s: %d, %u bytes -> 0x%08x\n", + S32G2_QSPI_PROGRAM_FILE, ret, (unsigned)n, + (uint32_t)S32G2_QSPI_PROGRAM_OFFSET); + if (n != 0) { + ret = s32g2_qspi_erase(S32G2_QSPI_PROGRAM_OFFSET, n); + if (ret == 0) + ret = s32g2_qspi_write(S32G2_QSPI_PROGRAM_OFFSET, img, n); + if (ret == 0) + ret = s32g2_qspi_read(S32G2_QSPI_PROGRAM_OFFSET, chk, n); + if (ret == 0 && memcmp(img, chk, n) != 0) + ret = -2; + wolfBoot_printf("s32g2: NOR program and read back: %s (%d)\n", + (ret == 0) ? "OK" : "FAILED", ret); + } + wolfBoot_printf("s32g2: NOR programmer done, halting\n"); + for (;;) + __asm__ volatile("wfi"); +} +#endif + +static void s32g2_soc_init(void) +{ + uintptr_t rgm = S32G2_MC_RGM_BASE; + int ret; + + /* Clear the reset-cause flags (write-1-to-clear) */ + wr32(rgm + MC_RGM_DES, rd32(rgm + MC_RGM_DES)); + wr32(rgm + MC_RGM_FES, rd32(rgm + MC_RGM_FES)); + + /* PMIC fail-safe watchdog and FCCU monitoring, before any clock moves: + * the VR5510 power-cycles the board on an FCCU error-pin event, and the + * console is still on FIRC so a failure here is reported. */ + ret = s32g2_pmic_init(); + if (ret != 0) { + wolfBoot_printf("s32g2: PMIC init failed (%d)\n", ret); + wolfBoot_panic(); + } + + /* The console moves with the LIN clock, so let it empty first. */ + linflex_drain(); + ret = s32g2_clk_init(); + if (ret != 0) { + wolfBoot_printf("s32g2: clock init failed at step %d\n", ret); + wolfBoot_panic(); + } + /* The counter ticks from FXOSC now; publish its rate. */ + __asm__ volatile("msr cntfrq_el0, %0" :: "r"((uint64_t)S32G2_CNTFRQ_HZ)); + + /* Caches on for everything from here, DDR training included. The + * coherent interconnect must be up before this core's caches. */ + ret = s32g2_ncore_init(); + if (ret != 0) { + wolfBoot_printf("s32g2: Ncore init failed (%d)\n", ret); + wolfBoot_panic(); + } + s32g2_el3_mmu_enable(); + + s32g2_dram_init(); + + /* Interrupt routing, the GIC for this core, and the three secondary + * cores parked at EL3 for PSCI. */ + s32g2_mscm_init(); + s32g2_gic_init_dist(); + s32g2_gic_init_cpu(); + if (s32g2_smp_release((uintptr_t)WOLFBOOT_ORIGIN) != 0) + wolfBoot_panic(); + + s32g2_sd_pads(); +#ifdef S32G2_QSPI + s32g2_qspi_pads(); + ret = s32g2_qspi_init(); + if (ret != 0) { + wolfBoot_printf("s32g2: QuadSPI init failed (%d)\n", ret); +#ifdef EXT_FLASH + /* The image partitions are in this flash. */ + wolfBoot_panic(); +#endif + } +#ifdef S32G2_QSPI_PROGRAM_FILE + s32g2_nor_program(ret); +#endif +#endif +} + +void hal_init(void) +{ + s32g2_console_init(); + wolfBoot_printf("wolfBoot S32G2 (EL%d, running from SRAM)\n", + (int)current_el()); + s32g2_soc_init(); +} + +void hal_prepare_boot(void) +{ +#if defined(DISK_SDCARD) || defined(DISK_EMMC) + /* Quiesce the controller so the OS does not inherit a uSDHC this + * firmware left clocked and running. */ + sdhci_shutdown(); +#endif +} + +/* No internal flash: wolfBoot runs from SRAM. Provide the required no-op + * HAL surface. */ +int RAMFUNCTION hal_flash_write(uintptr_t address, const uint8_t *data, int len) +{ + (void)address; (void)data; (void)len; + return 0; +} + +void RAMFUNCTION hal_flash_unlock(void) +{ +} + +void RAMFUNCTION hal_flash_lock(void) +{ +} + +int RAMFUNCTION hal_flash_erase(uintptr_t address, int len) +{ + (void)address; (void)len; + return 0; +} diff --git a/hal/s32g2.h b/hal/s32g2.h new file mode 100644 index 0000000000..12245e105f --- /dev/null +++ b/hal/s32g2.h @@ -0,0 +1,223 @@ +/* s32g2.h + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +/* NXP S32G2 (S32G274A) definitions for the S32G-VNP-GLDBOX / S32G274A-RDB2. + * boot_aarch64_start.S includes this, so keep assembly off the C-only parts. */ + +#ifndef _S32G2_H_ +#define _S32G2_H_ + +/* Simple AArch64 startup: wolfBoot is entered by the Boot ROM at EL3 in + * SRAM, with the SRAM initialisation added in front of the common path. */ +#define USE_BUILTIN_STARTUP +#define USE_SIMPLE_STARTUP + +/* wolfBoot runs with its identity MMU and caches on: build the D-cache + * maintenance by address range that the SDMA path needs. */ +#define WOLFBOOT_AARCH64_DCACHE_RANGE + +/* System SRAM, and its two controllers (4 MB each). An ECC read-modify-write + * on SRAM the controller has not initialised faults, so the startup code runs + * the controller over everything it uses beyond what the Boot ROM copied. The + * initialisation address of a byte offset is ((off >> 9) << 2) | ((off >> 4) & 3). */ +#define S32G2_SRAM_BASE 0x34000000 +#define S32G2_SRAM_SIZE 0x00800000 +#define S32G2_SRAMC0_BASE 0x4019C000 /* 0x34000000-0x343FFFFF */ +#define SRAMC_PRAMCR 0x00 +#define SRAMC_PRAMIAS 0x04 +#define SRAMC_PRAMIAE 0x08 +#define SRAMC_PRAMSR 0x0C +#define SRAMC_PRAMCR_INITREQ 1 +#define SRAMC_PRAMSR_IDONE 1 + +/* Reset Generation Module: destructive and functional reset-cause flags, + * write-1-to-clear. */ +#define S32G2_MC_RGM_BASE 0x40078000 +#define MC_RGM_DES 0x00 +#define MC_RGM_FES 0x08 + +/* SIUL2_0: chip identification and the pad muxing for UART0. */ +#define S32G2_SIUL2_0_MSCR_BASE 0x4009C240 /* MSCR n at + 4 * n */ +#define S32G2_SIUL2_0_IMCR_BASE 0x4009CA40 /* IMCR n at + 4 * (n - 512) */ +#define S32G2_UART0_TX_MSCR 41 /* PC09 */ +#define S32G2_UART0_RX_MSCR 42 /* PC10 */ +#define S32G2_UART0_RX_IMCR 512 +/* ALT1 | OBE | SRE 133 MHz; ALT0 | IBE | SRE; IMCR source ALT2 */ +#define S32G2_UART0_TX_MSCR_VAL 0x00218001 +#define S32G2_UART0_RX_MSCR_VAL 0x00098000 +#define S32G2_UART0_RX_IMCR_VAL 0x2 +/* uSDHC0 pads 46-56 (MSCR) and input muxes 515-524 (IMCR): ALT1 with the + * 150 MHz slew; CLK pull-down, CMD/DATA/VSELECT pull-up, RST output only. */ +#define S32G2_SD_CLK_MSCR_VAL 0x00296001 +#define S32G2_SD_DATA_MSCR_VAL 0x00297001 +#define S32G2_SD_RST_MSCR_VAL 0x00216001 +#define S32G2_SD_IMCR_VAL 0x2 + +/* QuadSPI: 64 MB NOR on bus A chip select 0; pads 85-93, 96-101 and input + * muxes 540-548. QSPI_2X is PERIPH DFS port 0 through MC_CGM0 mux 12, and + * the serial clock is half of it. */ +#define S32G2_QSPI_BASE 0x40134000 +#define S32G2_QSPI_AHB_BASE 0x00000000 +#define S32G2_QSPI_FLASH_SIZE 0x04000000 +#define S32G2_QSPI_DATA_MSCR_VAL 0x00282001 +#define S32G2_QSPI_CLK_MSCR_VAL 0x00200001 +#define S32G2_QSPI_CS_MSCR_VAL 0x00203001 +#define S32G2_QSPI_IMCR_VAL 0x2 +#ifndef S32G2_QSPI_2X_DIV +#define S32G2_QSPI_2X_DIV 8 /* 800 MHz / 8 = 100 MHz QSPI_2X, 50 MHz SCK */ +#endif + +/* Out of reset the LIN clock is the 48 MHz FIRC, until the clock tree is + * programmed: 48e6 / (16 * 115200) = 26.04. */ +#define S32G2_LINIBRR_115200_FIRC 26 +#define S32G2_LINFBRR_115200_FIRC 1 + +/* Clock generation: crystal oscillator, PLLs, DFS fractional dividers, the + * clock-generation modules and the mode-entry module's partition gating. */ +#define S32G2_FXOSC_BASE 0x40050000 +#define S32G2_ARM_PLL_BASE 0x40038000 +#define S32G2_PERIPH_PLL_BASE 0x4003C000 +#define S32G2_DDR_PLL_BASE 0x40044000 +#define S32G2_ARM_DFS_BASE 0x40054000 +#define S32G2_PERIPH_DFS_BASE 0x40058000 +#define S32G2_MC_CGM0_BASE 0x40030000 +#define S32G2_MC_CGM1_BASE 0x40034000 +#define S32G2_MC_CGM5_BASE 0x40068000 +#define S32G2_MC_ME_BASE 0x40088000 +#define S32G2_MC_ME_USDHC_BLOCK 0 /* partition 0, COFB block 0 */ +#define S32G2_MC_ME_DDR_BLOCK 1 /* partition 0, COFB block 1 */ +/* The system counter runs at FXOSC / 8 once the crystal is up. */ +#define S32G2_CNTFRQ_HZ 5000000 + +/* I2C4 and the VR5510 PMIC behind it. The controller clock is XBAR / 2 / 3. */ +#define S32G2_I2C4_BASE 0x402DC000 +#define S32G2_VR5510_FSU_ADDR 0x21 +#define S32G2_SIUL2_1_IMCR_BASE 0x44010C1C /* IMCR n at + 4 * (n - 631) */ +#define S32G2_SIUL2_1_IMCR_START 631 +#define S32G2_I2C4_SDA_MSCR 33 /* PC01 */ +#define S32G2_I2C4_SCL_MSCR 34 /* PC02 */ +#define S32G2_I2C4_SDA_IMCR 724 +#define S32G2_I2C4_SCL_IMCR 723 +/* ALT1/ALT2 | OBE | ODE | IBE | SRE 133 MHz; IMCR source ALT3 */ +#define S32G2_I2C4_SDA_MSCR_VAL 0x00398001 +#define S32G2_I2C4_SCL_MSCR_VAL 0x00398002 +#define S32G2_I2C4_IMCR_VAL 0x3 + +/* Interconnect, interrupt router and GICv3. */ +#define S32G2_NCORE_BASE 0x50400000 +#define S32G2_NCORE_CAIUTC 0x50400000 /* CAIU0 transaction control */ +#define S32G2_NCORE_CAIUTC_ISOLEN (1U << 1) +#define S32G2_MSCM_BASE 0x40198000 +#define S32G2_MSCM_IRSPRC 0x880 /* 16-bit routing per SPI */ +#define S32G2_MSCM_IRSPRC_NUM 240 +#define S32G2_GICD_BASE 0x50800000 +#define S32G2_GICR_BASE 0x50880000 +#define S32G2_GICR_STRIDE 0x20000 +#ifndef __ASSEMBLER__ /* src/boot_aarch64_start.S has its own GICD names */ +#define GICD_CTLR 0x000 +#define GICD_TYPER 0x004 +#define GICD_IGROUPR 0x080 +#define GICD_IGROUPMODR 0xD00 +#define GICD_CTLR_ENGRP0 (1U << 0) +#define GICD_CTLR_ENGRP1NS (1U << 1) +#define GICD_CTLR_ENGRP1S (1U << 2) +#define GICD_CTLR_ARE_S (1U << 4) +#define GICD_CTLR_ARE_NS (1U << 5) +#define GICR_TYPER 0x008 +#define GICR_WAKER 0x014 +#define GICR_WAKER_PROCESSORSLEEP (1U << 1) +#define GICR_WAKER_CHILDRENASLEEP (1U << 2) +#define GICR_SGI_BASE 0x10000 +#define GICR_IGROUPR0 0x080 +#define GICR_ISENABLER0 0x100 +#define GICR_IGROUPMODR0 0xD00 +#endif + +/* Per-core EL3 stacks below the image, indexed aff1 * 2 + aff0: secondary + * bring-up and the kernel's PSCI calls run on them. */ +#define S32G2_NUM_CORES 4 +#define S32G2_SRAM_LOW_INIT_END 0x34008000 +#define S32G2_EL3_STACK_TOP 0x34004000 /* 4 KB per core, downwards */ +#define S32G2_EL3_STACK_SIZE 0x1000 + +#ifndef __ASSEMBLER__ +int s32g2_cgm_mux_set(uintptr_t cgm, uint32_t mux, uint32_t src); +uint32_t s32g2_cgm_mux_get(uintptr_t cgm, uint32_t mux); +int s32g2_ddr_init(void); +int s32g2_ddr_polling_needed(void); +void hal_delay_us(uint32_t us); +#endif + +/* DRAM bank 0 at 2 GB, bank 1 at 34 GB. The sizes are the GoldBox's 4 GB + * LPDDR4 and must match what the DDR blob trains. */ +#define S32G2_DRAM_BASE 0x80000000 +#define S32G2_DRAM_BANK1_BASE 0x880000000ULL +#ifndef S32G2_DRAM_BANK0_SIZE +#define S32G2_DRAM_BANK0_SIZE 0x80000000ULL +#endif +#ifndef S32G2_DRAM_BANK1_SIZE +#define S32G2_DRAM_BANK1_SIZE 0x80000000ULL +#endif +#define S32G2_DRAM_SIZE (S32G2_DRAM_BANK0_SIZE + S32G2_DRAM_BANK1_SIZE) + +#define TIMER_CLK_FREQ S32G2_CNTFRQ_HZ + +/* Console LINFlexD_0 (ttyLF0): stdout-path in s32g274a-rdb2.dts, pins PC09/PC10, + * 115200 8N1. LINFlexD_1 at 0x401CC000 is the second FTDI port. */ +#define S32G2_LINFLEX0_BASE 0x401C8000 +#define LINFLEX_LINCR1 0x00 +#define LINFLEX_LINSR 0x08 +#define LINFLEX_UARTCR 0x10 +#define LINFLEX_UARTSR 0x14 +#define LINFLEX_LINFBRR 0x24 +#define LINFLEX_LINIBRR 0x28 +#define LINFLEX_BDRL 0x38 +#define LINFLEX_UARTPTO 0x50 + +#define LINFLEX_LINCR1_INIT (1 << 0) +#define LINFLEX_LINCR1_MME (1 << 4) +#define LINFLEX_LINSR_LINS_MASK (0xF << 12) +#define LINFLEX_LINSR_LINS_INIT (0x1 << 12) +#define LINFLEX_UARTCR_UART (1 << 0) +/* UART | WL0 | PC0 | TXEN | RXEN | PC1 | TFBM | RFBM: 8N1, FIFO mode on both + * directions, which is also how U-Boot and TF-A leave the port. */ +#define LINFLEX_UARTCR_8N1_FIFO 0x37B +#define LINFLEX_UARTSR_DTF (1 << 1) /* TX FIFO full (FIFO mode) */ + +/* LIN_BAUD clock as the earlier stage programs it (PERIPH_PLL_PHI3), and the + * 115200 divisor from it: 125e6 / (16 * 115200) = 67.8 -> 67 + 13/16. */ +#define S32G2_LINIBRR_115200 67 +#define S32G2_LINFBRR_115200 13 + +/* uSDHC0: the only SD/MMC host on S32G2. The GoldBox routes it to the microSD + * slot by default and to the on-board eMMC through a board mux. */ +#define S32G2_USDHC0_BASE 0x402F0000 + +/* uSDHC registers and the shim interface (hal/imx_usdhc_sdhci.c) */ +#include "imx_usdhc_sdhci.h" + +#define IMX_USDHC_BASE S32G2_USDHC0_BASE +#define IMX_USDHC_PERCLK_HZ 400000000 /* PERIPH_DFS3 / 2 */ +#ifdef S32G2_USDHC_MAX_CLK_KHZ +#define IMX_USDHC_MAX_CLK_KHZ S32G2_USDHC_MAX_CLK_KHZ +#endif + +#endif /* _S32G2_H_ */ diff --git a/hal/s32g2.its b/hal/s32g2.its new file mode 100644 index 0000000000..8ecfa60021 --- /dev/null +++ b/hal/s32g2.its @@ -0,0 +1,26 @@ +/dts-v1/; +/* + * hal/s32g2.its - FIT for the S32G2: kernel plus device tree, built and + * signed by tools/scripts/s32g2/s32g2-mkfit.sh. + * Copyright (C) 2026 wolfSSL Inc. GPLv3. + */ +/ { + description = "wolfBoot S32G2 (S32G274A) Linux + DTB"; + #address-cells = <1>; + images { + kernel-1 { + description = "Linux kernel (arm64, gzip)"; + data = /incbin/("../Image.gz"); + type = "kernel"; arch = "arm64"; os = "linux"; compression = "gzip"; + load = <0x84080000>; entry = <0x84080000>; + hash-1 { algo = "sha256"; }; + }; + fdt-1 { + description = "Flattened device tree"; + data = /incbin/("../s32g2.dtb"); + type = "flat_dt"; arch = "arm64"; compression = "none"; + hash-1 { algo = "sha256"; }; + }; + }; + configurations { default = "conf1"; conf1 { kernel = "kernel-1"; fdt = "fdt-1"; hash-1 { algo = "sha256"; }; }; }; +}; diff --git a/hal/s32g2.ld b/hal/s32g2.ld new file mode 100644 index 0000000000..99d60b4676 --- /dev/null +++ b/hal/s32g2.ld @@ -0,0 +1,74 @@ +/* s32g2.ld - link map for the NXP S32G2: wolfBoot is the Boot ROM's + * application image, copied into the 8 MB system SRAM and entered at + * WOLFBOOT_ORIGIN (0x340A0000) at EL3. Everything stays below 0x343F0000: + * inside SRAM controller 0's range, which the startup code ECC-initialises, + * and below the Boot ROM's reservation at 0x343FF000. + */ +MEMORY +{ + SRAM_MEM(rwx): ORIGIN = @WOLFBOOT_ORIGIN@, LENGTH = 0x343F0000 - @WOLFBOOT_ORIGIN@ +} +ENTRY(_vector_table); + +SECTIONS +{ + .text : + { + _start_text = .; + KEEP(*(.boot*)) + *(.text*) + *(.rodata*) + *(.note.*) + /* Whole pages: the EL3 map makes exactly this range read-only and + * executable, and nothing else executable. */ + . = ALIGN(4096); + _end_text = .; + } > SRAM_MEM + ASSERT((_end_text - 1) / 0x200000 == _start_text / 0x200000, + "s32g2: text must stay within one 2 MB block of the EL3 map") + .ARM.exidx : + { + . = ALIGN(4); + *(.ARM.exidx*) + } > SRAM_MEM + + .data : + { + PROVIDE(_stored_data = .); + _start_data = .; + KEEP(*(.data*)) + . = ALIGN(4); + KEEP(*(.ramcode)) + /* Loaded data too: left to follow _end_data, the table would sit + * in the range the startup clears, and a symbol reached through it + * (a weak one, for instance) would read as zero. */ + *(.got*) + /* The SRAM controller initialises 512-byte granules; the startup + * clears from here, so nothing loaded may share this granule. */ + . = ALIGN(512); + _end_data = .; + } > SRAM_MEM + + /* Not written by the Boot ROM: the startup code ECC-initialises + * _end_data to END_STACK before any store. */ + .bss (NOLOAD) : + { + _start_bss = .; + __bss_start__ = .; + *(.bss*) + *(COMMON) + . = ALIGN(4); + _end_bss = .; + __bss_end__ = .; + } > SRAM_MEM + + .stack (NOLOAD) : + { + . = ALIGN(16); + . += 0x40000; /* 256 KB: the ECC verify, FIT parse and inflate run here */ + . = ALIGN(16); + END_STACK = .; + _end = .; + } > SRAM_MEM + . = ALIGN(4); +} diff --git a/hal/s32g2_ddr.c b/hal/s32g2_ddr.c new file mode 100644 index 0000000000..5e15261739 --- /dev/null +++ b/hal/s32g2_ddr.c @@ -0,0 +1,486 @@ +/* s32g2_ddr.c + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +/* LPDDR4 bring-up for the NXP S32G2: a Synopsys uMCTL2 controller with a + * DWC LPDDR4 multiPHY. The sequence is the controller's and PHY's standard + * one: program the controller, release it from reset, load and run the PHY + * training firmware (1D then 2D), load the PHY init engine, then hand the + * DFI over and enter normal operation. + * + * The board's register tables and the PHY training firmware are not part of + * wolfBoot. They come from a data blob built by + * tools/scripts/s32g2/s32g2-ddr-blob.py and linked in with S32G2_DDR_BLOB. + * Inline ECC is not supported: the blob is generated without it. */ + +#include +#include +#include "hal/s32g2.h" +#include "hal.h" + +#if defined(DEBUG_UART) + #define PRINTF_ENABLED +#endif +#include "printf.h" + +static inline uint32_t rd32(uintptr_t a) { return *(volatile uint32_t*)a; } +static inline void wr32(uintptr_t a, uint32_t v) { *(volatile uint32_t*)a = v; } + +/* uMCTL2 controller */ +#define DDRC 0x403C0000UL +#define DDRC_STAT (DDRC + 0x004) +#define DDRC_STAT_OPMODE 0x7U +#define DDRC_MRCTRL0 (DDRC + 0x010) +#define DDRC_MRCTRL0_READ (1U << 0) +#define DDRC_MRCTRL0_RANK0 (1U << 4) +#define DDRC_MRCTRL0_RANK_MASK (0xFU << 4) +#define DDRC_MRCTRL0_START (1U << 31) +#define DDRC_MRCTRL1 (DDRC + 0x014) +#define DDRC_MRSTAT (DDRC + 0x018) +#define DDRC_MRSTAT_BUSY (1U << 0) +#define DDRC_DERATEEN (DDRC + 0x020) +#define DDRC_DERATEEN_EN (1U << 0) +#define DDRC_PWRCTL (DDRC + 0x030) +#define DDRC_PWRCTL_SELFREF_EN (1U << 0) +#define DDRC_PWRCTL_PDN_EN (1U << 1) +#define DDRC_PWRCTL_CLKDIS_EN (1U << 3) +#define DDRC_PWRCTL_SELFREF_SW (1U << 5) +#define DDRC_RFSHCTL3 (DDRC + 0x060) +#define DDRC_RFSHCTL3_DIS_AUTO (1U << 0) +#define DDRC_RFSHCTL3_UPDATE (1U << 1) +#define DDRC_RFSHTMG (DDRC + 0x064) +#define DDRC_ECCCFG0 (DDRC + 0x070) +#define DDRC_DRAMTMG0 (DDRC + 0x100) +#define DDRC_DRAMTMG1 (DDRC + 0x104) +#define DDRC_DRAMTMG4 (DDRC + 0x110) +#define DDRC_DFIMISC (DDRC + 0x1B0) +#define DDRC_DFIMISC_COMPLETE_EN (1U << 0) +#define DDRC_DFIMISC_INIT_START (1U << 5) +#define DDRC_DFISTAT (DDRC + 0x1BC) +#define DDRC_DFISTAT_COMPLETE (1U << 0) +#define DDRC_DBG1 (DDRC + 0x304) +#define DDRC_SWCTL (DDRC + 0x320) +#define DDRC_SWSTAT (DDRC + 0x324) +#define DDRC_PCTRL(n) (DDRC + 0x490 + (n) * 0xB0) +#define DDRC_NUM_AXI_PORTS 3 + +/* Performance block: mode register read data, and the LPDDR4 select */ +#define DDR_PERF_MRR0 0x403E0040UL +#define DDR_PERF_MRR1 0x403E0044UL + +/* Subsystem glue: AXI parity and DFI1 enable; controller reset; DDR clock */ +#define DDR_SS_REG 0x403D0000UL +#define DDR_SS_AXI_PARITY_EN 0x00001FF0U +#define DDR_SS_AXI_PARITY_ODD 0x01FF0000U +#define DDR_SS_DFI1_EN 0x00000001U +#define DDR_RGM_PRST0 0x40078040UL +#define DDR_RGM_PRST0_DDR (1U << 3) +#define DDR_CGM_MUX_FIRC 0 +#define DDR_CGM_MUX_PLL_PHI0 36 + +/* PHY: APB-only block (firmware mailbox, reset), master block, memories */ +#define PHY_MUX_SEL 0x40380400UL /* 0 = CSRs writable */ +#define PHY_UCT_SHADOW 0x40380404UL +#define PHY_UCT_SHADOW_MSG (1U << 0) /* 0 = a message is waiting */ +#define PHY_DCT_WRITE_PROT 0x4038040CUL +#define PHY_UCT_MSG 0x40380410UL +#define PHY_MICRO_RESET 0x40380420UL +#define PHY_CAL_BUSY 0x4038165CUL +#define PHY_PLL_CTRL2 0x403816DCUL +#define PHY_PLL_CTRL1 0x403816F0UL +#define PHY_PLL_TESTMODE 0x40381708UL +#define PHY_PLL_CTRL4 0x4038171CUL +#define PHY_IMEM 0x403A0000UL +#define PHY_DMEM 0x403B0000UL +#define PHY_MSG_DONE 0x07U +#define PHY_MSG_FAIL 0xFFU + +#define DDR_POLL_US 1000000U +#define DDR_MAX_MESSAGES 10000U + +/* Blob written by tools/scripts/s32g2/s32g2-ddr-blob.py */ +#define BLOB_MAGIC 0x44323353U /* "S32D" */ +#define BLOB_VERSION 1U +#define BLOB_HDR_WORDS 4 +enum { + BLOB_DDRC, BLOB_DQ_SWAP, BLOB_PHY, BLOB_PIE, + BLOB_IMEM_1D, BLOB_DMEM_1D, BLOB_IMEM_2D, BLOB_DMEM_2D, + BLOB_SECTIONS +}; + +#ifdef S32G2_DDR_BLOB +__asm__( + ".section .rodata.s32g2_ddr_blob, \"a\"\n" + ".balign 8\n" + ".global s32g2_ddr_blob\n" + "s32g2_ddr_blob:\n" + ".incbin \"" S32G2_DDR_BLOB "\"\n" + ".global s32g2_ddr_blob_end\n" + "s32g2_ddr_blob_end:\n" + ".previous\n"); +extern const uint8_t s32g2_ddr_blob[]; +extern const uint8_t s32g2_ddr_blob_end[]; +#endif + +static const uint32_t* blob; +static int errata_polling = -1; + +static int wait_bits(uintptr_t reg, uint32_t mask, uint32_t want) +{ + uint32_t us; + + for (us = 0; us < DDR_POLL_US; us++) { + if ((rd32(reg) & mask) == want) + return 0; + hal_delay_us(1); + } + return -1; +} + +static void set_bits(uintptr_t reg, uint32_t bits) +{ + wr32(reg, rd32(reg) | bits); +} + +static void clr_bits(uintptr_t reg, uint32_t bits) +{ + wr32(reg, rd32(reg) & ~bits); +} + +static uint32_t crc32(const uint8_t* p, uint32_t len) +{ + uint32_t crc = 0xFFFFFFFFU, i; + int b; + + for (i = 0; i < len; i++) { + crc ^= p[i]; + for (b = 0; b < 8; b++) + crc = (crc >> 1) ^ (0xEDB88320U & (0U - (crc & 1U))); + } + return ~crc; +} + +static int blob_open(void) +{ +#ifdef S32G2_DDR_BLOB + const uint32_t* b = (const uint32_t*)(uintptr_t)s32g2_ddr_blob; + uint32_t avail = (uint32_t)((uintptr_t)s32g2_ddr_blob_end - + (uintptr_t)s32g2_ddr_blob); + uint32_t total, i, off, count, bytes; + + if (avail < (BLOB_HDR_WORDS + 2 * BLOB_SECTIONS) * 4 || + b[0] != BLOB_MAGIC || b[1] != BLOB_VERSION) + return -1; + total = b[2]; + if (total > avail || total < (BLOB_HDR_WORDS + 2 * BLOB_SECTIONS) * 4) + return -1; + if (crc32((const uint8_t*)b + 16, total - 16) != b[3]) + return -2; + for (i = 0; i < BLOB_SECTIONS; i++) { + off = b[BLOB_HDR_WORDS + 2 * i]; + count = b[BLOB_HDR_WORDS + 2 * i + 1]; + bytes = (i < BLOB_IMEM_1D) ? 8 : 2; + if ((off & 3) != 0 || off > total || count > (total - off) / bytes) + return -3; + } + blob = b; + return 0; +#else + return -4; +#endif +} + +static const void* blob_section(int id, uint32_t* count) +{ + *count = blob[BLOB_HDR_WORDS + 2 * id + 1]; + return (const uint8_t*)blob + blob[BLOB_HDR_WORDS + 2 * id]; +} + +/* A table of {register address, value} writes */ +static void write_table(int id) +{ + uint32_t count, i; + const uint32_t* t = (const uint32_t*)blob_section(id, &count); + + for (i = 0; i < count; i++) + wr32((uintptr_t)t[2 * i], t[2 * i + 1]); +} + +/* 16-bit words into consecutive 32-bit PHY memory cells */ +static void load_phy_mem(uintptr_t dst, int id) +{ + uint32_t count, i; + const uint16_t* w = (const uint16_t*)blob_section(id, &count); + + wr32(PHY_MUX_SEL, 0); + for (i = 0; i < count; i++) + wr32(dst + 4 * i, w[i]); + wr32(PHY_MUX_SEL, 1); +} + +/* Quasi-dynamic register group: open with sw_done = 0, close with 1 */ +static int swctl(uint32_t done) +{ + wr32(DDRC_SWCTL, done); + return wait_bits(DDRC_SWSTAT, 1, done); +} + +/* Bring the controller out of reset with the DDR clock parked on FIRC, as + * the reset release needs, then quiesce it for PHY training. */ +static int controller_release(void) +{ + int moved = 0; + + set_bits(DDR_SS_REG, DDR_SS_AXI_PARITY_EN); + set_bits(DDR_SS_REG, DDR_SS_AXI_PARITY_ODD); + set_bits(DDR_SS_REG, DDR_SS_DFI1_EN); + + if (s32g2_cgm_mux_get(S32G2_MC_CGM5_BASE, 0) != DDR_CGM_MUX_FIRC) { + if (s32g2_cgm_mux_set(S32G2_MC_CGM5_BASE, 0, DDR_CGM_MUX_FIRC) != 0) + return -10; + moved = 1; + } + clr_bits(DDR_RGM_PRST0, DDR_RGM_PRST0_DDR); + if (moved && + s32g2_cgm_mux_set(S32G2_MC_CGM5_BASE, 0, DDR_CGM_MUX_PLL_PHI0) != 0) + return -11; + + wr32(DDRC_DBG1, 0); + set_bits(DDRC_RFSHCTL3, DDRC_RFSHCTL3_DIS_AUTO); + clr_bits(DDRC_PWRCTL, DDRC_PWRCTL_PDN_EN); + clr_bits(DDRC_PWRCTL, DDRC_PWRCTL_SELFREF_EN); + clr_bits(DDRC_PWRCTL, DDRC_PWRCTL_CLKDIS_EN); + if (swctl(0) != 0) + return -12; + clr_bits(DDRC_DFIMISC, DDRC_DFIMISC_COMPLETE_EN); + if (swctl(1) != 0) + return -13; + return 0; +} + +/* Start the PHY microcontroller on the loaded firmware and follow its + * mailbox until it reports the training stage done or failed. */ +static int run_training(void) +{ + uint32_t n, msg; + + /* PHY PLL settings for this data rate, then reset and release */ + wr32(PHY_PLL_CTRL1, 0x21); + wr32(PHY_PLL_TESTMODE, 0x24); + wr32(PHY_PLL_CTRL4, 0x17F); + wr32(PHY_PLL_CTRL2, 0x19); + wr32(PHY_MUX_SEL, 1); + wr32(PHY_MICRO_RESET, 0x9); + wr32(PHY_MICRO_RESET, 0x1); + wr32(PHY_MICRO_RESET, 0x0); + + for (n = 0; n < DDR_MAX_MESSAGES; n++) { + if (wait_bits(PHY_UCT_SHADOW, PHY_UCT_SHADOW_MSG, 0) != 0) + return -20; + msg = rd32(PHY_UCT_MSG); + /* Acknowledge, wait for the firmware to take it, re-arm */ + wr32(PHY_DCT_WRITE_PROT, 0); + if (wait_bits(PHY_UCT_SHADOW, PHY_UCT_SHADOW_MSG, + PHY_UCT_SHADOW_MSG) != 0) + return -21; + wr32(PHY_DCT_WRITE_PROT, 1); + if (msg == PHY_MSG_DONE) + return 0; + if (msg == PHY_MSG_FAIL) + return -22; + } + return -23; +} + +static int phy_train(void) +{ + uint32_t count; + int ret; + + write_table(BLOB_DQ_SWAP); + write_table(BLOB_PHY); + + load_phy_mem(PHY_IMEM, BLOB_IMEM_1D); + load_phy_mem(PHY_DMEM, BLOB_DMEM_1D); + wolfBoot_printf("DDR: 1D training\n"); + ret = run_training(); + if (ret != 0) + return ret; + + (void)blob_section(BLOB_IMEM_2D, &count); + if (count != 0) { + load_phy_mem(PHY_IMEM, BLOB_IMEM_2D); + load_phy_mem(PHY_DMEM, BLOB_DMEM_2D); + wolfBoot_printf("DDR: 2D training\n"); + ret = run_training(); + if (ret != 0) + return ret - 10; + } + + /* PHY init engine sequences, with the CSRs writable */ + wr32(PHY_MUX_SEL, 0); + write_table(BLOB_PIE); + wr32(PHY_MUX_SEL, 1); + return 0; +} + +/* Read an LPDDR4 mode register of rank 0 through the controller */ +static int mode_reg_read(uint32_t mr, uint32_t* val) +{ + uint32_t v; + + set_bits(DDR_PERF_MRR0, 1); + if (wait_bits(DDRC_MRSTAT, DDRC_MRSTAT_BUSY, 0) != 0) + return -1; + v = rd32(DDRC_MRCTRL0) & ~DDRC_MRCTRL0_RANK_MASK; + wr32(DDRC_MRCTRL0, v | DDRC_MRCTRL0_READ | DDRC_MRCTRL0_RANK0); + wr32(DDRC_MRCTRL1, (rd32(DDRC_MRCTRL1) & ~0xFF00U) | (mr << 8)); + __asm__ volatile("dsb sy"); + set_bits(DDRC_MRCTRL0, DDRC_MRCTRL0_START); + if (wait_bits(DDRC_MRSTAT, DDRC_MRSTAT_BUSY, 0) != 0) + return -1; + *val = rd32(DDR_PERF_MRR1); + return 0; +} + +/* Add delta to a timing field, failing instead of overflowing it */ +static int field_add(uintptr_t reg, uint32_t shift, uint32_t mask, + uint32_t delta) +{ + uint32_t v = rd32(reg); + uint32_t f = ((v >> shift) & mask) + delta; + + if (f > mask) + return -1; + wr32(reg, (v & ~(mask << shift)) | (f << shift)); + return 0; +} + +/* NXP erratum ERR050543: the controller's temperature derating can use a + * stale MR4 refresh-rate code. Read MR4; when the device asks for derated + * timing (code above 3), switch automatic derating off and apply the + * derated refresh interval and AC timings by hand; otherwise automatic + * derating is safe to enable. An OS then has to keep polling MR4. */ +static int errata_050543(void) +{ + uint32_t mr4, code, v; + + if (mode_reg_read(4, &mr4) != 0) + return -30; + code = mr4 & 0x7; + if (((mr4 >> 16) & 0x7) > code) + code = (mr4 >> 16) & 0x7; + + if (code <= 3) { + set_bits(DDRC_DERATEEN, DDRC_DERATEEN_EN); + errata_polling = 0; + return 0; + } + clr_bits(DDRC_DERATEEN, DDRC_DERATEEN_EN); + /* t_rfc_nom_x1_x32 to a quarter, flagged through refresh_update_level */ + v = rd32(DDRC_RFSHTMG); + wr32(DDRC_RFSHTMG, (v & ~(0xFFFU << 16)) | + ((((v >> 16) & 0xFFFU) >> 2) << 16)); + wr32(DDRC_RFSHCTL3, rd32(DDRC_RFSHCTL3) ^ DDRC_RFSHCTL3_UPDATE); + if (swctl(0) != 0) + return -31; + if (field_add(DDRC_DRAMTMG4, 24, 0x1F, 2) != 0 || /* t_rcd */ + field_add(DDRC_DRAMTMG4, 8, 0xF, 2) != 0 || /* t_rrd */ + field_add(DDRC_DRAMTMG0, 0, 0x3F, 2) != 0 ||/* t_ras_min */ + field_add(DDRC_DRAMTMG4, 0, 0x1F, 2) != 0 ||/* t_rp */ + field_add(DDRC_DRAMTMG1, 0, 0x7F, 3) != 0) /* t_rc */ + return -32; + if (swctl(1) != 0) + return -33; + errata_polling = 1; + return 0; +} + +/* DFI initialisation, normal operating mode, low-power features, AXI ports */ +static int controller_start(void) +{ + uint32_t n; + int ret; + + if (wait_bits(PHY_CAL_BUSY, 1, 0) != 0) + return -40; + if (swctl(0) != 0) + return -41; + set_bits(DDRC_DFIMISC, DDRC_DFIMISC_INIT_START); + if (swctl(1) != 0) + return -42; + if (wait_bits(DDRC_DFISTAT, DDRC_DFISTAT_COMPLETE, + DDRC_DFISTAT_COMPLETE) != 0) + return -43; + if (swctl(0) != 0) + return -44; + clr_bits(DDRC_DFIMISC, DDRC_DFIMISC_INIT_START); + set_bits(DDRC_DFIMISC, DDRC_DFIMISC_COMPLETE_EN); + clr_bits(DDRC_PWRCTL, DDRC_PWRCTL_SELFREF_SW); + if (swctl(1) != 0) + return -45; + + for (n = 0; n < DDR_POLL_US; n++) { + if ((rd32(DDRC_STAT) & DDRC_STAT_OPMODE) != 0) + break; + hal_delay_us(1); + } + if (n == DDR_POLL_US) + return -46; + clr_bits(DDRC_RFSHCTL3, DDRC_RFSHCTL3_DIS_AUTO); + if ((rd32(DDRC_ECCCFG0) & 0x7) != 0) + return -47; /* inline ECC tables: not supported */ + set_bits(DDRC_PWRCTL, DDRC_PWRCTL_PDN_EN); + set_bits(DDRC_PWRCTL, DDRC_PWRCTL_SELFREF_EN); + set_bits(DDRC_PWRCTL, DDRC_PWRCTL_CLKDIS_EN); + + ret = errata_050543(); + if (ret != 0) + return ret; + for (n = 0; n < DDRC_NUM_AXI_PORTS; n++) + wr32(DDRC_PCTRL(n), 1); + return 0; +} + +int s32g2_ddr_init(void) +{ + int ret; + + ret = blob_open(); + if (ret != 0) { + wolfBoot_printf("DDR: no valid data blob (%d), build with " + "S32G2_DDR_BLOB\n", ret); + return ret; + } + write_table(BLOB_DDRC); + ret = controller_release(); + if (ret == 0) + ret = phy_train(); + if (ret == 0) + ret = controller_start(); + return ret; +} + +/* 1 when ERR050543 left derating to be handled by polling MR4, 0 when + * automatic derating is on, -1 before s32g2_ddr_init() succeeded. */ +int s32g2_ddr_polling_needed(void) +{ + return errata_polling; +} diff --git a/src/boot_aarch64.c b/src/boot_aarch64.c index 3407776ab8..307578bd09 100644 --- a/src/boot_aarch64.c +++ b/src/boot_aarch64.c @@ -263,6 +263,15 @@ void RAMFUNCTION do_boot(const uint32_t *app_offset) asm volatile("isb"); #endif +#if defined(TARGET_s32g2) + /* wolfBoot runs at EL3 on this target; the payload is entered at EL2. */ + { + extern void s32g2_el3_to_el2_boot(uintptr_t entry, uintptr_t dts) + __attribute__((noreturn)); + s32g2_el3_to_el2_boot((uintptr_t)app_offset, (uintptr_t)dts_offset); + } +#endif + /* Set application address via x4 */ asm volatile("mov x4, %0" : : "r"(app_offset)); diff --git a/src/boot_aarch64_start.S b/src/boot_aarch64_start.S index 9641a9acfb..eac830a95f 100644 --- a/src/boot_aarch64_start.S +++ b/src/boot_aarch64_start.S @@ -62,6 +62,10 @@ #include "hal/imx8mm.h" #endif +#ifdef TARGET_s32g2 +#include "hal/s32g2.h" +#endif + /* VA region the TCR_EL{1,2,3} T0SZ values below cover. A high DDR window * must fit inside it; keep in step with those TCR literals. */ #ifndef WOLFBOOT_AARCH64_VA_LIMIT @@ -1322,6 +1326,34 @@ _cm4_entry: 9: #endif +#if defined(TARGET_s32g2) + /* CPUECTLR.SMPEN, which the A53 wants before any cache use. */ + mrs x2, S3_1_C15_C2_1 + orr x2, x2, #(1 << 6) + msr S3_1_C15_C2_1, x2 + isb + mrs x3, mpidr_el1 + and x3, x3, #0xFFFF + cbnz x3, s32g2_secondary_entry /* secondaries: released by the primary */ + /* The Boot ROM ECC-initialised only what it copied: initialise .bss, the + * stack and the per-core EL3 stacks before any store. */ + ldr x2, =_end_data + ldr x3, =END_STACK + bl s32g2_sramc0_clear + ldr x2, =S32G2_SRAM_BASE + ldr x3, =S32G2_SRAM_LOW_INIT_END + bl s32g2_sramc0_clear + /* The instruction cache, or every fetch goes to SRAM over the + * interconnect. */ + ic iallu + dsb sy + isb + mrs x2, sctlr_el3 + orr x2, x2, #(1 << 12) + msr sctlr_el3, x2 + isb +#endif + 4: ldr x1, =_vector_table // get start of .text in x1 // Read current EL mrs x0, CurrentEL @@ -1335,6 +1367,11 @@ _cm4_entry: msr scr_el3, x2 msr cptr_el3, xzr // enable FP/SIMD +#if defined(TARGET_s32g2) + ldr x2, =s32g2_el3_vectors + msr VBAR_EL3, x2 + isb +#endif // EL == 1? 2: cmp x0, #4 @@ -1370,7 +1407,7 @@ _cm4_entry: 8: #if defined(TARGET_tegra234) || defined(TARGET_imx8qm) || \ - defined(TARGET_imx95_a55) || \ + defined(TARGET_imx95_a55) || defined(TARGET_s32g2) || \ (defined(TARGET_imx8mm) && defined(IMX8MM_BL33)) /* Reserved in-DRAM stack from the linker script: the generic path sets sp * to the image base, which here is the BL33 load address. */ @@ -1489,6 +1526,188 @@ simple_el2_fault_stack_top: .section ".boot" #endif #endif /* TARGET_cm4 || TARGET_imx8qm || (TARGET_imx8mm && IMX8MM_BL33) */ + +#if defined(TARGET_s32g2) +/* EL3 exception vectors: wolfBoot is entered by the Boot ROM at EL3 with no + * usable VBAR_EL3. After the handoff the same table takes the kernel's SMCs + * (lower EL, AArch64, synchronous); everything else reports and halts. */ +.align 11 +.global s32g2_el3_vectors +s32g2_el3_vectors: +.irp idx, 0, 1, 2, 3, 4, 5, 6, 7 + .align 7 + mov x3, #\idx + b s32g2_el3_fault_common +.endr + .align 7 + b s32g2_smc_entry +.irp idx, 9, 10, 11, 12, 13, 14, 15 + .align 7 + mov x3, #\idx + b s32g2_el3_fault_common +.endr + +s32g2_el3_fault_common: +#if defined(DEBUG) && defined(DEBUG_UART) + ldr x4, =END_STACK + mov sp, x4 + mrs x0, esr_el3 + mrs x1, elr_el3 + mrs x2, far_el3 + bl s32g2_el3_fault_handler /* void (esr, elr, far, vector) */ +#endif +9: wfi + b 9b + +/* x2 = start, x3 = end: ECC-initialise the 512-byte granules of SRAM + * controller 0 covering [x2, x3). Clobbers x2-x8, no stack. */ +s32g2_sramc0_clear: + ldr x4, =S32G2_SRAM_BASE + sub x2, x2, x4 + sub x3, x3, x4 + and x2, x2, #~511 + add x3, x3, #511 + and x3, x3, #~511 + sub x3, x3, #1 + /* initialisation address = ((off >> 9) << 2) | ((off >> 4) & 3): the low + * field is 0 at a granule's first byte and 3 at its last */ + lsr x5, x2, #9 + lsl x5, x5, #2 + lsr x6, x3, #9 + lsl x6, x6, #2 + orr x6, x6, #3 + ldr x4, =S32G2_SRAMC0_BASE + str wzr, [x4, #SRAMC_PRAMCR] + str w5, [x4, #SRAMC_PRAMIAS] + str w6, [x4, #SRAMC_PRAMIAE] + mov w7, #SRAMC_PRAMCR_INITREQ + str w7, [x4, #SRAMC_PRAMCR] + mov x8, #0x1000000 /* bound on the wait for IDONE */ +6: ldr w7, [x4, #SRAMC_PRAMSR] + tbnz w7, #0, 7f + subs x8, x8, #1 + b.ne 6b + /* Not initialised: the stack and .bss would fault on first use. */ +8: wfi + b 8b +7: + mov w7, #SRAMC_PRAMSR_IDONE + str w7, [x4, #SRAMC_PRAMSR] + ret + +/* sp = the calling core's EL3 stack top, \id = its linear core id */ +.macro S32G2_EL3_STACK id, t1, t2 + mrs \t1, mpidr_el1 + and \id, \t1, #0x3 + ubfx \t1, \t1, #8, #8 + add \id, \id, \t1, lsl #1 /* id = aff1 * 2 + aff0 */ + ldr \t1, =S32G2_EL3_STACK_TOP + mov \t2, #S32G2_EL3_STACK_SIZE + msub \t1, \id, \t2, \t1 + mov sp, \t1 +.endm + +/* SMC from the kernel: x0-x3 go to the PSCI dispatcher and its result comes + * back in x0, with the other registers as the caller left them (SMCCC 1.1): + * x4-x18 and x30 are saved here, the C code preserves x19-x29. Runs on the + * core's own EL3 stack, which the last entry to EL2 left empty. */ +s32g2_smc_entry: + sub sp, sp, #0x80 + stp x4, x5, [sp, #0x00] + stp x6, x7, [sp, #0x10] + stp x8, x9, [sp, #0x20] + stp x10, x11, [sp, #0x30] + stp x12, x13, [sp, #0x40] + stp x14, x15, [sp, #0x50] + stp x16, x17, [sp, #0x60] + stp x18, x30, [sp, #0x70] + mrs x9, esr_el3 + lsr x9, x9, #26 + cmp x9, #0x17 /* EC: SMC from AArch64 */ + b.ne 1f + bl wolfBoot_psci_smc + ldp x4, x5, [sp, #0x00] + ldp x6, x7, [sp, #0x10] + ldp x8, x9, [sp, #0x20] + ldp x10, x11, [sp, #0x30] + ldp x12, x13, [sp, #0x40] + ldp x14, x15, [sp, #0x50] + ldp x16, x17, [sp, #0x60] + ldp x18, x30, [sp, #0x70] + add sp, sp, #0x80 + mov x1, xzr + mov x2, xzr + mov x3, xzr + eret +1: mov x3, #8 + b s32g2_el3_fault_common + +/* Secondary cores enter here at EL3 once the primary releases them: EL3 + * state, their SRAM stack, the per-core setup in C, then the parking loop. */ +s32g2_secondary_entry: + ldr x2, =s32g2_el3_vectors + msr VBAR_EL3, x2 + mrs x2, scr_el3 + orr x2, x2, #0x0F /* NS | IRQ | FIQ | EA */ + msr scr_el3, x2 + msr cptr_el3, xzr + isb + S32G2_EL3_STACK x19, x2, x3 + bl s32g2_secondary_main + /* fall through */ + +/* s32g2_psci_park(): the calling core waits at EL3, on a fresh stack, until + * the kernel turns it on, and then enters the kernel at EL2. Also where + * CPU_OFF ends. Does not return. */ +.global s32g2_psci_park +s32g2_psci_park: + S32G2_EL3_STACK x19, x2, x3 + mov x0, x19 + bl s32g2_psci_wait /* x0 = entry point, x1 = context id */ + mov x20, x0 + mov x0, x1 + /* fall through */ + +/* Enter the payload at EL2 (MMU off, DAIF masked) at x20 with x0 as its + * argument, leaving this core's EL3 stack empty for its SMCs. */ +s32g2_enter_el2: + /* NS | EA | HCE | RW, with IRQ and FIQ left to EL2: routing them to EL3 + * makes the GICv3 trap the OS's group register accesses. */ + mov x2, #0x509 + msr scr_el3, x2 + mov x2, #(1 << 31) /* HCR_EL2.RW */ + msr hcr_el2, x2 + /* RES1 bits only: a core that was turned off comes back with whatever + * the kernel last had here. */ + ldr x2, =0x30C50830 + msr sctlr_el2, x2 + mov x2, #0x3C9 /* EL2h, DAIF masked */ + msr spsr_el3, x2 + msr elr_el3, x20 + S32G2_EL3_STACK x1, x2, x3 + mov x1, xzr + mov x2, xzr + mov x3, xzr + isb + eret + +/* s32g2_el3_to_el2_boot(entry, dtb): enter the payload at EL2 from EL3 with + * the arm64 boot contract (MMU off, DAIF masked, x0 = DTB). */ +.global s32g2_el3_to_el2_boot +s32g2_el3_to_el2_boot: + msr daifset, #0xf + mov x20, x0 + mov x21, x1 + mov x0, xzr + bl s32g2_el3_mmu_map_dram + /* wolfBoot loaded the payload through its own cached mapping: write it + * all back. Registers only from here on. The shared routine cleans and + * invalidates the caches and clears SCTLR_EL2; the EL3 MMU and caches + * stay on for the PSCI runtime, which lives in SRAM. */ + bl el2_flush_and_disable_mmu + mov x0, x21 + b s32g2_enter_el2 +#endif /* TARGET_s32g2 */ #if defined(TARGET_cm4) /* Firmware-provided DTB pointer, stored at _cm4_entry. Read by hal_get_boot_dts() * (hal/cm4.c) for the kernel-only FIT boot path. In .data so it is not subject to diff --git a/tools/scripts/s32g2/s32g2-ddr-blob.py b/tools/scripts/s32g2/s32g2-ddr-blob.py new file mode 100755 index 0000000000..ce632fac6c --- /dev/null +++ b/tools/scripts/s32g2/s32g2-ddr-blob.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python3 +# s32g2-ddr-blob.py - build the LPDDR4 data blob wolfBoot needs on the S32G2. +# +# s32g2-ddr-blob.py +# +# Packs the S32G274A rev 2 register tables and the PHY training firmware from +# an NXP BSP U-Boot checkout (board/freescale/s32-gen1, BSP 28 to 30) into +# the binary hal/s32g2_ddr.c consumes, with CONFIG_S32GEN1_DRAM_INLINE_ECC +# unset. See docs/Targets.md. +# +# Blob layout, little-endian 32-bit words: +# magic "S32D", version, total size, CRC-32 of everything after this word, +# then eight {offset, count} descriptors and the data they point at: +# 0 controller table {address, value} pairs +# 1 DQ swap table {address, value} pairs +# 2 PHY table {address, value} pairs +# 3 PHY init engine table {address, value} pairs +# 4 1D training code 16-bit words +# 5 1D training data 16-bit words +# 6 2D training code 16-bit words +# 7 2D training data 16-bit words +# +# Copyright (C) 2026 wolfSSL Inc. GPLv3. + +import os +import re +import struct +import sys +import zlib + +MAGIC = 0x44323353 # "S32D" +VERSION = 1 +NUM_SECTIONS = 8 +ECC_MACRO = "CONFIG_S32GEN1_DRAM_INLINE_ECC" + + +def strip_conditionals(text): + """Keep the #else side of every #ifdef ECC_MACRO block, drop comments.""" + text = re.sub(r"/\*.*?\*/", "", text, flags=re.S) + out = [] + skip = [] + for line in text.splitlines(): + s = line.strip() + if s.startswith("#ifdef") and ECC_MACRO in s: + skip.append(True) + elif s.startswith("#else") and skip: + skip[-1] = not skip[-1] + elif s.startswith("#endif") and skip: + skip.pop() + elif s.startswith("#"): + continue + elif not (skip and skip[-1]): + out.append(line) + return "\n".join(out) + + +def array_numbers(path, name): + with open(path, "r", errors="replace") as f: + text = strip_conditionals(f.read()) + m = re.search(r"\b%s\s*\[\s*\]\s*=\s*\{(.*?)\}\s*;" % re.escape(name), + text, flags=re.S) + if m is None: + sys.exit("%s: array %s not found" % (path, name)) + return [int(v, 0) for v in re.findall(r"0[xX][0-9a-fA-F]+|\b\d+\b", + m.group(1))] + + +def pairs(path, name): + n = array_numbers(path, name) + if len(n) == 0 or len(n) % 2 != 0: + sys.exit("%s: %s is not a list of {address, value}" % (path, name)) + return struct.pack("<%dI" % len(n), *n), len(n) // 2 + + +def words16(path, name): + n = array_numbers(path, name) + if len(n) == 0 or max(n) > 0xFFFF: + sys.exit("%s: %s is not a list of 16-bit words" % (path, name)) + data = struct.pack("<%dH" % len(n), *n) + return data + b"\0" * (-len(data) % 4), len(n) + + +def main(argv): + soc = "s32g274a" + rev = "rev2" + args = argv[1:] + if len(args) != 2: + sys.stderr.write("usage: s32g2-ddr-blob.py \n") + return 2 + base = os.path.join(args[0], "board", "freescale", "s32-gen1") + socdir = os.path.join(base, soc) + revdir = os.path.join(socdir, rev) + sfx = "_" + rev + if not os.path.isdir(revdir): + sys.exit("%s: not found (is this the NXP BSP U-Boot tree?)" % revdir) + + sections = [ + pairs(os.path.join(revdir, "ddrc_cfg%s.c" % sfx), "ddrc_cfg" + sfx), + pairs(os.path.join(socdir, "dq_swap_cfg.c"), "dq_swap_cfg"), + pairs(os.path.join(revdir, "phy_cfg%s.c" % sfx), "phy_cfg" + sfx), + pairs(os.path.join(revdir, "pie_cfg%s.c" % sfx), "pie_cfg" + sfx), + words16(os.path.join(base, "imem_cfg.c"), "imem_1d"), + words16(os.path.join(revdir, "dmem_cfg%s.c" % sfx), "dmem_1d" + sfx), + words16(os.path.join(base, "imem_cfg.c"), "imem_2d"), + words16(os.path.join(revdir, "dmem_cfg%s.c" % sfx), "dmem_2d" + sfx), + ] + assert len(sections) == NUM_SECTIONS + + off = 16 + 8 * NUM_SECTIONS + table = b"" + body = b"" + for data, count in sections: + table += struct.pack(" +# +# Output: s32g2-fitImage_v_signed.bin in the wolfBoot root. Copy it +# to the FAT32 boot partition as /fitImage_a.bin or /fitImage_b.bin +# (config/examples/s32g2-goldbox.config), or program it into a NOR partition. +# The file ends with 4 KB of padding: on a file slot wolfBoot keeps the boot +# confirmation state in its last bytes (DISK_BOOT_CONFIRM). +set -e +ROOT="$(cd "$(dirname "$0")/../../.." && pwd)" +USAGE="usage: $0 " +KERNEL="${1:?$USAGE}" +VERSION="${2:?$USAGE}" +KEY="${3:?$USAGE}" +DTB="${4:?$USAGE}" +ITS="$ROOT/hal/s32g2.its" +SIGN_OPTS="${SIGN_OPTS:---ecc384 --sha384}" + +[ -f "$KERNEL" ] || { echo "!! kernel not found: $KERNEL"; exit 1; } +[ -f "$KEY" ] || { echo "!! signing key not found: $KEY"; exit 1; } +# sign runs from the wolfBoot root: resolve the key against the caller's +# directory first. +KEY="$(cd "$(dirname "$KEY")" && pwd)/$(basename "$KEY")" +command -v mkimage >/dev/null || { echo "!! mkimage not found (u-boot-tools)"; exit 1; } + +case "$KERNEL" in + *.gz) [ "$KERNEL" -ef "$ROOT/Image.gz" ] || cp -L "$KERNEL" "$ROOT/Image.gz" ;; + *) gzip -9 -c "$KERNEL" > "$ROOT/Image.gz" ;; +esac +[ -f "$DTB" ] || { echo "!! dtb not found: $DTB"; exit 1; } +[ "$DTB" -ef "$ROOT/s32g2.dtb" ] || cp -L "$DTB" "$ROOT/s32g2.dtb" +echo "== building FIT ($ITS) ==" +mkimage -f "$ITS" "$ROOT/s32g2-fitImage" >/dev/null +echo "== signing the FIT (v$VERSION, $SIGN_OPTS) ==" +( cd "$ROOT" && ./tools/keytools/sign $SIGN_OPTS s32g2-fitImage "$KEY" "$VERSION" >/dev/null ) +OUT="$ROOT/s32g2-fitImage_v${VERSION}_signed.bin" +[ -f "$OUT" ] || { echo "!! signing failed"; exit 1; } +head -c 4096 /dev/zero >> "$OUT" +ls -l "$OUT" diff --git a/tools/scripts/s32g2/s32g2_mkimage.py b/tools/scripts/s32g2/s32g2_mkimage.py new file mode 100755 index 0000000000..ae8d8c7165 --- /dev/null +++ b/tools/scripts/s32g2/s32g2_mkimage.py @@ -0,0 +1,148 @@ +#!/usr/bin/env python3 +# s32g2_mkimage.py - wrap wolfboot.bin for the S32G2 Boot ROM. +# +# s32g2_mkimage.py [--qspi [--qspi-params sdr|octal]] +# +# Default: the SD/eMMC content from byte 0x1000 (dd with seek=8), in the stock +# BSP layout: IVT, no-op DCD, application header, code 0x200-aligned. +# --qspi: the NOR content from offset 0, with the ROM's QuadSPI parameter +# block at 0x200. "sdr" (default) reads the flash in plain SPI mode, as +# wolfBoot's driver does; "octal" is the stock MX25UW51245G block, which +# switches the flash to octal DTR. +# +# The ROM copies code_length bytes from behind the 0x40-byte header to +# ram_start, which is the entry minus the pad between header and code. +# +# Copyright (C) 2026 wolfSSL Inc. GPLv3. + +import struct +import sys + +SD_IVT_OFFSET = 0x1000 # where this file lands on the card +ENTRY = 0x340A0000 # WOLFBOOT_ORIGIN +QSPI_PARAMS_OFF = 0x0200 # QuadSPI boot only, fixed by the ROM +DCD_OFF = 0x0200 +QSPI_DCD_OFF = 0x0400 +APP_OFF = 0x2200 # application boot code header +CODE_ALIGN = 0x200 +APP_HDR_SIZE = 0x40 + +SRAM_BASE = 0x34000000 +SRAM_SIZE = 0x00800000 +# Boot ROM working areas while it loads from SD; the image must not be copied +# over them (U-Boot tools/s32gen1image.c). +SD_RESERVED = [(0x34008000, 0x34078000), (0x343FF000, 0x34400000)] + + +def ivt(dcd_ptr, app_ptr): + b = bytearray(0x100) + b[0] = 0xD1 # tag + b[1:3] = (0x100).to_bytes(2, "big") # length, big-endian + b[3] = 0x60 # version + struct.pack_into(" " + "[--qspi [--qspi-params sdr|octal]]\n") + return 2 + entry = ENTRY + qspi = "--qspi" in argv + params = "sdr" + if "--qspi-params" in argv: + params = argv[argv.index("--qspi-params") + 1] + base = 0 if qspi else SD_IVT_OFFSET + dcd_off = QSPI_DCD_OFF if qspi else DCD_OFF + with open(argv[1], "rb") as f: + code = f.read() + + code_off = APP_OFF + APP_HDR_SIZE + code_aligned = (code_off + CODE_ALIGN - 1) & ~(CODE_ALIGN - 1) + pad = code_aligned - code_off + ram_start = entry - pad + # Partial 64-byte SRAM lines fault on ECC, then whole 512-byte sectors. + length = (pad + len(code) + 63) & ~63 + length = (length + 511) & ~511 + + end = ram_start + length + if ram_start < SRAM_BASE or end > SRAM_BASE + SRAM_SIZE: + sys.stderr.write("image does not fit in SRAM\n") + return 1 + for lo, hi in SD_RESERVED: + if ram_start < hi and end > lo: + sys.stderr.write("image overlaps the Boot ROM reserved SRAM %#x-%#x\n" + % (lo, hi)) + return 1 + + out = bytearray(code_aligned + length - pad) + out[0:0x100] = ivt(base + dcd_off, base + APP_OFF) + if qspi: + out[QSPI_PARAMS_OFF:QSPI_PARAMS_OFF + 0x200] = qspi_params(params) + out[dcd_off:dcd_off + 0x14] = dcd_nop() + out[APP_OFF:APP_OFF + APP_HDR_SIZE] = app_header(ram_start, entry, length) + out[code_aligned:code_aligned + len(code)] = code + with open(argv[2], "wb") as f: + f.write(out) + print("%s: %d bytes, ram_start %#x entry %#x code_length %#x (%s offset %#x)" + % (argv[2], len(out), ram_start, entry, length, + "QSPI %s" % params if qspi else "SD", base)) + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv))