From ed935c66cb37ee5dc6b6d9266c92e193c6b6a641 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 1 Oct 2025 02:57:32 +0800 Subject: [PATCH] loader tracking api changes; needs refinement still. --- loader/Cargo.toml | 14 +- loader/src/platform/bao1x/bao1x.rs | 11 +- loader/src/platform/bao1x/link.x | 358 +++++++++++----------- loader/src/platform/bao1x/update.rs | 6 +- loader/src/platform/bao1x/usb/handlers.rs | 8 +- 5 files changed, 205 insertions(+), 192 deletions(-) diff --git a/loader/Cargo.toml b/loader/Cargo.toml index a442dcac9..02ced4473 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -14,6 +14,10 @@ ed25519-dalek-loader = { path = "ed25519-dalek-loader", default-features = false curve25519-dalek-loader = { path = "curve25519-dalek-loader", default-features = false, features = [ "u32e_backend", ], optional = true } +ed25519-dalek-bao1x = { path = "../bao1x-boot/ed25519-dalek-bao1x", default-features = false, features = [ + "digest", +], optional = true } +sha2-bao1x = { path = "../bao1x-boot/sha2-bao1x", optional = true, default-features = false } utralib = { version = "0.1.25", optional = true, default-features = false } armv7 = { git = "https://github.com/Foundation-Devices/armv7.git", branch = "update", optional = true } atsama5d27 = { git = "https://github.com/Foundation-Devices/atsama5d27.git", branch = "master", optional = true } @@ -100,13 +104,17 @@ board-baosec = [ "usb", "simple-fatfs", "linked_list_allocator", - "ed25519-dalek", - "sha2", + "ed25519-dalek-bao1x", + "sha2-bao1x", "digest", "ux-api/loader-baosec", ] board-baosor = ["bao1x-hal/loader-baosor"] -board-dabao = ["bao1x-hal/loader-dabao", "bao1x-hal/board-dabao", "linked_list_allocator",] +board-dabao = [ + "bao1x-hal/loader-dabao", + "bao1x-hal/board-dabao", + "linked_list_allocator", +] atsama5d27 = ["utralib/atsama5d27", "armv7", "dep:atsama5d27"] # precursor flags diff --git a/loader/src/platform/bao1x/bao1x.rs b/loader/src/platform/bao1x/bao1x.rs index 85fc0f6d1..cc75eeed7 100644 --- a/loader/src/platform/bao1x/bao1x.rs +++ b/loader/src/platform/bao1x/bao1x.rs @@ -1,3 +1,4 @@ +use bao1x_api::signatures::SIGBLOCK_LEN; #[cfg(not(feature = "verilator-only"))] use bao1x_api::{ udma::{PeriphId, UdmaGlobalConfig}, @@ -93,7 +94,7 @@ pub fn delay(quantum: usize) { pub fn early_init() -> u32 { // For the loader, the statics structure is located just after the signature block - const STATICS_LOC: usize = FLASH_BASE + 0x1000; + const STATICS_LOC: usize = bao1x_api::LOADER_START + SIGBLOCK_LEN; // safety: this data structure is pre-loaded by the image loader and is guaranteed to // only have representable, valid values that are aligned according to the repr(C) spec @@ -112,7 +113,9 @@ pub fn early_init() -> u32 { data_ptr.add(i).write_volatile(0); } for &(offset, data) in &statics_in_rom.poke_table[..statics_in_rom.valid_pokes as usize] { - data_ptr.add(offset as usize).write_volatile(data); + data_ptr + .add(u16::from_le_bytes(offset) as usize / size_of::()) + .write_volatile(u32::from_le_bytes(data)); } } @@ -286,8 +289,8 @@ pub fn early_init_hw() -> u32 { let mut iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); // setup power to "shut down" - keyboard press should be what's keeping us on at this stage - let (port, pin) = bao1x_hal::board::setup_keep_on_pin(&iox); - iox.set_gpio_pin_value(port, pin, IoxValue::Low); + // let (port, pin) = bao1x_hal::board::setup_keep_on_pin(&iox); + // iox.set_gpio_pin_value(port, pin, IoxValue::Low); /* // allow for shutdown to process iox.setup_pin( diff --git a/loader/src/platform/bao1x/link.x b/loader/src/platform/bao1x/link.x index db09be8d5..3f41ff539 100644 --- a/loader/src/platform/bao1x/link.x +++ b/loader/src/platform/bao1x/link.x @@ -1,179 +1,179 @@ -MEMORY -{ - FLASH : ORIGIN = 0x60001100, LENGTH = 252k - 256 - RAM : ORIGIN = 0x61000000, LENGTH = 2M -} - -REGION_ALIAS("REGION_TEXT", FLASH); -REGION_ALIAS("REGION_RODATA", FLASH); -REGION_ALIAS("REGION_DATA", RAM); -REGION_ALIAS("REGION_BSS", RAM); -REGION_ALIAS("REGION_STACK", RAM); -REGION_ALIAS("REGION_HEAP", RAM); - -/* Size of the main kernel stack */ -_stack_size = 16K; -_eheap = ORIGIN(RAM) + LENGTH(RAM); - - -PROVIDE(_stext = ORIGIN(REGION_TEXT)); -PROVIDE(_stack_start = ORIGIN(REGION_STACK) + LENGTH(REGION_STACK)); -PROVIDE(_max_hart_id = 0); -PROVIDE(_hart_stack_size = 2K); -PROVIDE(_heap_size = 0); - -PROVIDE(trap_handler = default_trap_handler); - -/* # Pre-initialization function */ -/* If the user overrides this using the `#[pre_init]` attribute or by creating a `__pre_init` function, - then the function this points to will be called before the RAM is initialized. */ -PROVIDE(__pre_init = default_pre_init); - -/* # Multi-processing hook function - fn _mp_hook() -> bool; - - This function is called from all the harts and must return true only for one hart, - which will perform memory initialization. For other harts it must return false - and implement wake-up in platform-dependent way (e.g. after waiting for a user interrupt). -*/ -PROVIDE(_mp_hook = default_mp_hook); - -ENTRY(_start) - -SECTIONS -{ - .text.dummy (NOLOAD) : - { - /* This section is intended to make _stext address work */ - . = ABSOLUTE(_stext); - } > REGION_TEXT - - .text _stext : - { - /* Put reset handler first in .text section so it ends up as the entry */ - /* point of the program. */ - KEEP(*(.text.init)); - KEEP(*(.init)); - KEEP(*(.init.rust)); - . = ALIGN(4); - KEEP(*(.trap)); - KEEP(*(.trap.rust)); - - *(.text .text.*); - } > REGION_TEXT - - .rodata : ALIGN(4) - { - *(.rodata .rodata.*); - - /* 4-byte align the end (VMA) of this section. - This is required by LLD to ensure the LMA of the following .data - section will have the correct alignment. */ - . = ALIGN(4); - _etext = .; - } > REGION_RODATA - - .data : ALIGN(4) - { - _sidata = LOADADDR(.data); - _sdata = .; - /* Must be called __global_pointer$ for linker relaxations to work. */ - PROVIDE(__global_pointer$ = . + 0x800); - *(.sdata .sdata.* .sdata2 .sdata2.*); - *(.data .data.*); - . = ALIGN(4); - _edata = .; - } > REGION_DATA AT > REGION_RODATA - - .bss (NOLOAD) : - { - _sbss = .; - *(.sbss .sbss.* .bss .bss.*); - . = ALIGN(4); - _ebss = .; - } > REGION_BSS - - /* fictitious region that represents the memory available for the stack */ - .stack (NOLOAD) : - { - _sstack = .; - . += _stack_size; - . = ALIGN(4096); - _estack = .; - } > REGION_STACK - - /* fictitious region that represents the memory available for the heap */ - .heap (NOLOAD) : - { - . = ALIGN(4); - _sheap = .; - /* _eheap is defined elsewhere and is the remainder of RAM */ - } > REGION_HEAP - - /* fake output .got section */ - /* Dynamic relocations are unsupported. This section is only used to detect - relocatable code in the input files and raise an error if relocatable code - is found */ - .got (INFO) : - { - KEEP(*(.got .got.*)); - } - - /* Discard .eh_frame, we are not doing unwind on panic so it is not needed */ - /DISCARD/ : - { - *(.eh_frame); - *(.eh_frame_hdr); - } -} - -/* Do not exceed this mark in the error messages above | */ -ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " -ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); - -ASSERT(ORIGIN(REGION_RODATA) % 4 == 0, " -ERROR(riscv-rt): the start of the REGION_RODATA must be 4-byte aligned"); - -ASSERT(ORIGIN(REGION_DATA) % 4 == 0, " -ERROR(riscv-rt): the start of the REGION_DATA must be 4-byte aligned"); - -ASSERT(ORIGIN(REGION_HEAP) % 4 == 0, " -ERROR(riscv-rt): the start of the REGION_HEAP must be 4-byte aligned"); - -ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " -ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); - -ASSERT(ORIGIN(REGION_STACK) % 4 == 0, " -ERROR(riscv-rt): the start of the REGION_STACK must be 4-byte aligned"); - -ASSERT(_stext % 4 == 0, " -ERROR(riscv-rt): `_stext` must be 4-byte aligned"); - -ASSERT(_sdata % 4 == 0 && _edata % 4 == 0, " -BUG(riscv-rt): .data is not 4-byte aligned"); - -ASSERT(_sidata % 4 == 0, " -BUG(riscv-rt): the LMA of .data is not 4-byte aligned"); - -ASSERT(_sbss % 4 == 0 && _ebss % 4 == 0, " -BUG(riscv-rt): .bss is not 4-byte aligned"); - -ASSERT(_sheap % 4 == 0, " -BUG(riscv-rt): start of .heap is not 4-byte aligned"); - -ASSERT(_stext + SIZEOF(.text) < ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT), " -ERROR(riscv-rt): The .text section must be placed inside the REGION_TEXT region. -Set _stext to an address smaller than 'ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT)'"); - -ASSERT(SIZEOF(.stack) > (_max_hart_id + 1) * _hart_stack_size, " -ERROR(riscv-rt): .stack section is too small for allocating stacks for all the harts. -Consider changing `_max_hart_id` or `_hart_stack_size`."); - -ASSERT(SIZEOF(.got) == 0, " -.got section detected in the input files. Dynamic relocations are not -supported. If you are linking to C code compiled using the `gcc` crate -then modify your build script to compile the C code _without_ the --fPIC flag. See the documentation of the `gcc::Config.fpic` method for -details."); - -/* Do not exceed this mark in the error messages above | */ +MEMORY +{ + FLASH : ORIGIN = 0x60060400, LENGTH = 255k + RAM : ORIGIN = 0x61000000, LENGTH = 2M +} + +REGION_ALIAS("REGION_TEXT", FLASH); +REGION_ALIAS("REGION_RODATA", FLASH); +REGION_ALIAS("REGION_DATA", RAM); +REGION_ALIAS("REGION_BSS", RAM); +REGION_ALIAS("REGION_STACK", RAM); +REGION_ALIAS("REGION_HEAP", RAM); + +/* Size of the main kernel stack */ +_stack_size = 16K; +_eheap = ORIGIN(RAM) + LENGTH(RAM); + + +PROVIDE(_stext = ORIGIN(REGION_TEXT)); +PROVIDE(_stack_start = ORIGIN(REGION_STACK) + LENGTH(REGION_STACK)); +PROVIDE(_max_hart_id = 0); +PROVIDE(_hart_stack_size = 2K); +PROVIDE(_heap_size = 0); + +PROVIDE(trap_handler = default_trap_handler); + +/* # Pre-initialization function */ +/* If the user overrides this using the `#[pre_init]` attribute or by creating a `__pre_init` function, + then the function this points to will be called before the RAM is initialized. */ +PROVIDE(__pre_init = default_pre_init); + +/* # Multi-processing hook function + fn _mp_hook() -> bool; + + This function is called from all the harts and must return true only for one hart, + which will perform memory initialization. For other harts it must return false + and implement wake-up in platform-dependent way (e.g. after waiting for a user interrupt). +*/ +PROVIDE(_mp_hook = default_mp_hook); + +ENTRY(_start) + +SECTIONS +{ + .text.dummy (NOLOAD) : + { + /* This section is intended to make _stext address work */ + . = ABSOLUTE(_stext); + } > REGION_TEXT + + .text _stext : + { + /* Put reset handler first in .text section so it ends up as the entry */ + /* point of the program. */ + KEEP(*(.text.init)); + KEEP(*(.init)); + KEEP(*(.init.rust)); + . = ALIGN(4); + KEEP(*(.trap)); + KEEP(*(.trap.rust)); + + *(.text .text.*); + } > REGION_TEXT + + .rodata : ALIGN(4) + { + *(.rodata .rodata.*); + + /* 4-byte align the end (VMA) of this section. + This is required by LLD to ensure the LMA of the following .data + section will have the correct alignment. */ + . = ALIGN(4); + _etext = .; + } > REGION_RODATA + + .data : ALIGN(4) + { + _sidata = LOADADDR(.data); + _sdata = .; + /* Must be called __global_pointer$ for linker relaxations to work. */ + PROVIDE(__global_pointer$ = . + 0x800); + *(.sdata .sdata.* .sdata2 .sdata2.*); + *(.data .data.*); + . = ALIGN(4); + _edata = .; + } > REGION_DATA AT > REGION_RODATA + + .bss (NOLOAD) : + { + _sbss = .; + *(.sbss .sbss.* .bss .bss.*); + . = ALIGN(4); + _ebss = .; + } > REGION_BSS + + /* fictitious region that represents the memory available for the stack */ + .stack (NOLOAD) : + { + _sstack = .; + . += _stack_size; + . = ALIGN(4096); + _estack = .; + } > REGION_STACK + + /* fictitious region that represents the memory available for the heap */ + .heap (NOLOAD) : + { + . = ALIGN(4); + _sheap = .; + /* _eheap is defined elsewhere and is the remainder of RAM */ + } > REGION_HEAP + + /* fake output .got section */ + /* Dynamic relocations are unsupported. This section is only used to detect + relocatable code in the input files and raise an error if relocatable code + is found */ + .got (INFO) : + { + KEEP(*(.got .got.*)); + } + + /* Discard .eh_frame, we are not doing unwind on panic so it is not needed */ + /DISCARD/ : + { + *(.eh_frame); + *(.eh_frame_hdr); + } +} + +/* Do not exceed this mark in the error messages above | */ +ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_RODATA) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_RODATA must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_DATA) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_DATA must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_HEAP) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_HEAP must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_STACK) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_STACK must be 4-byte aligned"); + +ASSERT(_stext % 4 == 0, " +ERROR(riscv-rt): `_stext` must be 4-byte aligned"); + +ASSERT(_sdata % 4 == 0 && _edata % 4 == 0, " +BUG(riscv-rt): .data is not 4-byte aligned"); + +ASSERT(_sidata % 4 == 0, " +BUG(riscv-rt): the LMA of .data is not 4-byte aligned"); + +ASSERT(_sbss % 4 == 0 && _ebss % 4 == 0, " +BUG(riscv-rt): .bss is not 4-byte aligned"); + +ASSERT(_sheap % 4 == 0, " +BUG(riscv-rt): start of .heap is not 4-byte aligned"); + +ASSERT(_stext + SIZEOF(.text) < ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT), " +ERROR(riscv-rt): The .text section must be placed inside the REGION_TEXT region. +Set _stext to an address smaller than 'ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT)'"); + +ASSERT(SIZEOF(.stack) > (_max_hart_id + 1) * _hart_stack_size, " +ERROR(riscv-rt): .stack section is too small for allocating stacks for all the harts. +Consider changing `_max_hart_id` or `_hart_stack_size`."); + +ASSERT(SIZEOF(.got) == 0, " +.got section detected in the input files. Dynamic relocations are not +supported. If you are linking to C code compiled using the `gcc` crate +then modify your build script to compile the C code _without_ the +-fPIC flag. See the documentation of the `gcc::Config.fpic` method for +details."); + +/* Do not exceed this mark in the error messages above | */ \ No newline at end of file diff --git a/loader/src/platform/bao1x/update.rs b/loader/src/platform/bao1x/update.rs index 4485b34b9..964778bcd 100644 --- a/loader/src/platform/bao1x/update.rs +++ b/loader/src/platform/bao1x/update.rs @@ -9,8 +9,8 @@ use bao1x_hal::sh1107::{Mono, Oled128x128}; use bao1x_hal::udma; use bao1x_hal::udma::*; use bao1x_hal::usb::driver::UsbDeviceState; -use ed25519_dalek::{Digest, Signature, VerifyingKey}; -use sha2::Sha512; +use ed25519_dalek_bao1x::{Digest, Signature, VerifyingKey}; +use sha2_bao1x::Sha512; use simple_fatfs::PathBuf; use utralib::generated::*; use ux_api::minigfx::{FrameBuffer, Point}; @@ -31,6 +31,8 @@ const TEXT_MIDLINE: isize = 51; const DISCONNECT_STATE: u32 = 0x40b; // 01_0_0000_0_1_01_1 const DISCONNECT_STATE_HS: u32 = 0xc6b; // 11_0_0011_0_1_01_1 +///// TODO: move updates into boot1. Make loader "generic" and just focus on loading the Xous kernel. + // loader is not updateable here because we're XIP. But we can update these other images: const SWAP_NAME: &'static str = "SWAP.IMG"; const KERNEL_NAME: &'static str = "XOUS.BIN"; diff --git a/loader/src/platform/bao1x/usb/handlers.rs b/loader/src/platform/bao1x/usb/handlers.rs index 508c392dc..0d9636815 100644 --- a/loader/src/platform/bao1x/usb/handlers.rs +++ b/loader/src/platform/bao1x/usb/handlers.rs @@ -304,7 +304,7 @@ pub fn usb_ep1_bulk_in_complete( if let Some((offset, len)) = this.remaining_rd.take() { let app_buf = conjure_app_buf(); let disk = conjure_disk(); - this.setup_big_read(app_buf, disk, offset, len); + this.setup_big_read(app_buf, disk, offset, len, None); this.ms_state = UmsState::DataPhase; } else { this.bulk_xfer(1, USB_SEND, CSW_ADDR, 13, 0, 0); @@ -652,7 +652,7 @@ fn process_read_command(this: &mut CorigineUsb, cbw: Cbw) { } let app_buf = conjure_app_buf(); let disk = conjure_disk(); - this.setup_big_read(app_buf, disk, lba as usize * SECTOR_SIZE as usize, length as usize); + this.setup_big_read(app_buf, disk, lba as usize * SECTOR_SIZE as usize, length as usize, None); this.ms_state = UmsState::DataPhase; } } @@ -675,7 +675,7 @@ fn process_write_command(this: &mut CorigineUsb, cbw: Cbw) { csw.status = 2; let app_buf = conjure_app_buf(); let disk = conjure_disk(); - this.setup_big_read(app_buf, disk, lba as usize * SECTOR_SIZE as usize, length as usize); + this.setup_big_read(app_buf, disk, lba as usize * SECTOR_SIZE as usize, length as usize, None); this.ms_state = UmsState::DataPhase; csw.update_hw(); return; @@ -739,7 +739,7 @@ fn process_write12_command(this: &mut CorigineUsb, cbw: Cbw) { // does let app_buf = conjure_app_buf(); let disk = conjure_disk(); - this.setup_big_read(app_buf, disk, lba as usize * SECTOR_SIZE as usize, length as usize); + this.setup_big_read(app_buf, disk, lba as usize * SECTOR_SIZE as usize, length as usize, None); this.ms_state = UmsState::DataPhase; csw.update_hw(); return;