From 3717c7ec2da7958ad323b0e2dcbf117185b54f30 Mon Sep 17 00:00:00 2001 From: Chris Frantz Date: Tue, 26 Mar 2024 11:25:54 -0700 Subject: [PATCH 1/4] [opentitantool] Improve rescue CLI 1. Enhance the boot-svc commands to retrieve and display the response. 2. Emit any boot-svc response `status` as hex. Signed-off-by: Chris Frantz (cherry picked from commit 4c593f5a2b92c321db62cc144933d6d62ca4f04f) (cherry picked from commit fea62b313d6a12a9c3459403dcbda028e466a5b3) --- .../silicon_creator/rom_ext/e2e/rescue/BUILD | 2 +- sw/host/opentitanlib/src/chip/boot_svc.rs | 4 + sw/host/opentitanlib/src/rescue/serial.rs | 6 +- sw/host/opentitantool/src/command/rescue.rs | 115 ++++++++++++++++-- 4 files changed, 112 insertions(+), 15 deletions(-) diff --git a/sw/device/silicon_creator/rom_ext/e2e/rescue/BUILD b/sw/device/silicon_creator/rom_ext/e2e/rescue/BUILD index fb6e242cd7327..f254a8885a9c7 100644 --- a/sw/device/silicon_creator/rom_ext/e2e/rescue/BUILD +++ b/sw/device/silicon_creator/rom_ext/e2e/rescue/BUILD @@ -95,7 +95,7 @@ opentitan_test( --exec="fpga load-bitstream {bitstream}" --exec="bootstrap --clear-uart=true {firmware}" # Set next slot via the rescue protocol - --exec="rescue boot-svc set-next-bl0-slot --next=SlotB" + --exec="rescue boot-svc set-next-bl0-slot --next=SlotB --get-response=false" # Check for firmware execution in slot B --exec="console --non-interactive --exit-success='bl0_slot = __BB\r\n' --exit-failure='{exit_failure}'" # Reset and observe return to slot A. diff --git a/sw/host/opentitanlib/src/chip/boot_svc.rs b/sw/host/opentitanlib/src/chip/boot_svc.rs index e65e598792b9c..c70a678dfed99 100644 --- a/sw/host/opentitanlib/src/chip/boot_svc.rs +++ b/sw/host/opentitanlib/src/chip/boot_svc.rs @@ -86,6 +86,7 @@ pub struct MinBl0SecVerResponse { /// The current minimum BL0 version. pub ver: u32, /// The status response to the request. + #[annotate(format = hex)] pub status: u32, } @@ -102,6 +103,7 @@ pub struct NextBl0SlotRequest { #[derive(Debug, Default, Serialize, Annotate)] pub struct NextBl0SlotResponse { /// The status response to the request. + #[annotate(format = hex)] pub status: u32, /// The current primary slot. pub primary_bl0_slot: BootSlot, @@ -129,6 +131,7 @@ pub struct OwnershipUnlockRequest { #[derive(Debug, Default, Serialize, Annotate)] pub struct OwnershipUnlockResponse { /// The status response to the request. + #[annotate(format = hex)] pub status: u32, } @@ -154,6 +157,7 @@ pub struct OwnershipActivateRequest { #[derive(Debug, Default, Serialize, Annotate)] pub struct OwnershipActivateResponse { /// The status response to the request. + #[annotate(format = hex)] pub status: u32, } diff --git a/sw/host/opentitanlib/src/rescue/serial.rs b/sw/host/opentitanlib/src/rescue/serial.rs index 18f2141d92694..d04678545bde6 100644 --- a/sw/host/opentitanlib/src/rescue/serial.rs +++ b/sw/host/opentitanlib/src/rescue/serial.rs @@ -47,10 +47,12 @@ impl RescueSerial { } } - pub fn enter(&self, transport: &TransportWrapper) -> Result<()> { + pub fn enter(&self, transport: &TransportWrapper, reset_target: bool) -> Result<()> { log::info!("Setting serial break to trigger rescue mode."); self.uart.set_break(true)?; - transport.reset_target(self.reset_delay, /*clear_uart*=*/ true)?; + if reset_target { + transport.reset_target(self.reset_delay, /*clear_uart=*/ true)?; + } UartConsole::wait_for(&*self.uart, r"rescue:.*\r\n", self.enter_delay)?; log::info!("Rescue triggered. clearing serial break."); self.uart.set_break(false)?; diff --git a/sw/host/opentitantool/src/command/rescue.rs b/sw/host/opentitantool/src/command/rescue.rs index 0d3ab43c4ade2..4883029bb0b69 100644 --- a/sw/host/opentitantool/src/command/rescue.rs +++ b/sw/host/opentitantool/src/command/rescue.rs @@ -46,6 +46,13 @@ pub struct Firmware { help = "Wait after upload (no automatic reboot)" )] wait: bool, + #[arg( + long, + default_value_t = true, + action = clap::ArgAction::Set, + help = "Reset the target to enter rescue mode" + )] + reset_target: bool, #[arg(value_name = "FILE")] filename: PathBuf, } @@ -77,7 +84,7 @@ impl CommandDispatch for Firmware { let uart = self.params.create(transport)?; let mut prev_baudrate = 0u32; let rescue = RescueSerial::new(Rc::clone(&uart)); - rescue.enter(transport)?; + rescue.enter(transport, self.reset_target)?; if let Some(rate) = self.rate { prev_baudrate = uart.get_baudrate()?; rescue.set_baud(rate)?; @@ -101,6 +108,13 @@ impl CommandDispatch for Firmware { pub struct GetBootLog { #[command(flatten)] params: UartParams, + #[arg( + long, + default_value_t = true, + action = clap::ArgAction::Set, + help = "Reset the target to enter rescue mode" + )] + reset_target: bool, #[arg(long, short, default_value = "false")] raw: bool, } @@ -113,7 +127,7 @@ impl CommandDispatch for GetBootLog { ) -> Result>> { let uart = self.params.create(transport)?; let rescue = RescueSerial::new(uart); - rescue.enter(transport)?; + rescue.enter(transport, self.reset_target)?; if self.raw { let data = rescue.get_boot_log_raw()?; Ok(Some(Box::new(RawBytes(data)))) @@ -128,6 +142,13 @@ impl CommandDispatch for GetBootLog { pub struct GetBootSvc { #[command(flatten)] params: UartParams, + #[arg( + long, + default_value_t = true, + action = clap::ArgAction::Set, + help = "Reset the target to enter rescue mode" + )] + reset_target: bool, #[arg(long, short, default_value = "false")] raw: bool, } @@ -140,7 +161,7 @@ impl CommandDispatch for GetBootSvc { ) -> Result>> { let uart = self.params.create(transport)?; let rescue = RescueSerial::new(uart); - rescue.enter(transport)?; + rescue.enter(transport, self.reset_target)?; if self.raw { let data = rescue.get_boot_svc_raw()?; Ok(Some(Box::new(RawBytes(data)))) @@ -169,6 +190,20 @@ pub struct SetNextBl0Slot { help = "Set the one-time next boot slot" )] next: BootSlot, + #[arg( + long, + default_value_t = true, + action = clap::ArgAction::Set, + help = "Reset the target to enter rescue mode" + )] + reset_target: bool, + #[arg( + long, + default_value_t = true, + action = clap::ArgAction::Set, + help = "Get the response from the target" + )] + get_response: bool, } impl CommandDispatch for SetNextBl0Slot { @@ -179,9 +214,16 @@ impl CommandDispatch for SetNextBl0Slot { ) -> Result>> { let uart = self.params.create(transport)?; let rescue = RescueSerial::new(uart); - rescue.enter(transport)?; + rescue.enter(transport, self.reset_target)?; rescue.set_next_bl0_slot(self.primary, self.next)?; - Ok(None) + if self.get_response { + rescue.enter(transport, false)?; + let response = rescue.get_boot_svc()?; + rescue.reboot()?; + Ok(Some(Box::new(response))) + } else { + Ok(None) + } } } @@ -189,6 +231,20 @@ impl CommandDispatch for SetNextBl0Slot { pub struct OwnershipUnlock { #[command(flatten)] params: UartParams, + #[arg( + long, + default_value_t = true, + action = clap::ArgAction::Set, + help = "Reset the target to enter rescue mode" + )] + reset_target: bool, + #[arg( + long, + default_value_t = true, + action = clap::ArgAction::Set, + help = "Get the response from the target" + )] + get_response: bool, #[command(flatten)] unlock: OwnershipUnlockParams, #[arg(short, long, help = "A file containing a binary unlock request")] @@ -207,9 +263,16 @@ impl CommandDispatch for OwnershipUnlock { let uart = self.params.create(transport)?; let rescue = RescueSerial::new(uart); - rescue.enter(transport)?; + rescue.enter(transport, self.reset_target)?; rescue.ownership_unlock(unlock)?; - Ok(None) + if self.get_response { + rescue.enter(transport, false)?; + let response = rescue.get_boot_svc()?; + rescue.reboot()?; + Ok(Some(Box::new(response))) + } else { + Ok(None) + } } } @@ -217,6 +280,20 @@ impl CommandDispatch for OwnershipUnlock { pub struct OwnershipActivate { #[command(flatten)] params: UartParams, + #[arg( + long, + default_value_t = true, + action = clap::ArgAction::Set, + help = "Reset the target to enter rescue mode" + )] + reset_target: bool, + #[arg( + long, + default_value_t = true, + action = clap::ArgAction::Set, + help = "Get the response from the target" + )] + get_response: bool, #[command(flatten)] activate: OwnershipActivateParams, #[arg(short, long, help = "A file containing a binary activate request")] @@ -235,9 +312,16 @@ impl CommandDispatch for OwnershipActivate { let uart = self.params.create(transport)?; let rescue = RescueSerial::new(uart); - rescue.enter(transport)?; + rescue.enter(transport, self.reset_target)?; rescue.ownership_activate(activate)?; - Ok(None) + if self.get_response { + rescue.enter(transport, false)?; + let response = rescue.get_boot_svc()?; + rescue.reboot()?; + Ok(Some(Box::new(response))) + } else { + Ok(None) + } } } @@ -245,6 +329,13 @@ impl CommandDispatch for OwnershipActivate { pub struct SetOwnerConfig { #[command(flatten)] params: UartParams, + #[arg( + long, + default_value_t = true, + action = clap::ArgAction::Set, + help = "Reset the target to enter rescue mode" + )] + reset_target: bool, #[arg(help = "A signed owner configuration block")] input: PathBuf, } @@ -258,14 +349,14 @@ impl CommandDispatch for SetOwnerConfig { let data = std::fs::read(&self.input)?; let uart = self.params.create(transport)?; let rescue = RescueSerial::new(uart); - rescue.enter(transport)?; + rescue.enter(transport, self.reset_target)?; rescue.set_owner_config(&data)?; Ok(None) } } #[derive(Debug, Subcommand, CommandDispatch)] -pub enum BootSvc { +pub enum BootSvcCommand { Get(GetBootSvc), SetNextBl0Slot(SetNextBl0Slot), OwnershipUnlock(OwnershipUnlock), @@ -275,7 +366,7 @@ pub enum BootSvc { #[derive(Debug, Subcommand, CommandDispatch)] pub enum RescueCommand { #[command(subcommand)] - BootSvc(BootSvc), + BootSvc(BootSvcCommand), GetBootLog(GetBootLog), Firmware(Firmware), SetOwnerConfig(SetOwnerConfig), From 7928cde776a94e3d84fe2908ab03c0cd4f8b446e Mon Sep 17 00:00:00 2001 From: Chris Frantz Date: Fri, 26 Apr 2024 09:19:27 -0700 Subject: [PATCH 2/4] [ownership] Add a `dummy` owner The `dummy` owner is a secondary owner used for testing ownership transfer flows. Signed-off-by: Chris Frantz (cherry picked from commit e5c2899e04b4dcc0f1b166aa3bcfdbcee32e16eb) (cherry picked from commit ba2766fb64dec44cc77a5f0ab939a3271b4830b2) --- .../lib/ownership/keys/dummy/BUILD | 30 ++++++++++++++++++ .../keys/dummy/activate_ecdsa_p256.der | Bin 0 -> 138 bytes .../keys/dummy/activate_ecdsa_p256.pub.der | Bin 0 -> 91 bytes .../keys/dummy/app_prod_rsa_3072_exp_f4.der | Bin 0 -> 1793 bytes .../dummy/app_prod_rsa_3072_exp_f4.pub.der | Bin 0 -> 398 bytes .../ownership/keys/dummy/owner_ecdsa_p256.der | Bin 0 -> 138 bytes .../keys/dummy/owner_ecdsa_p256.pub.der | Bin 0 -> 91 bytes .../keys/dummy/unlock_ecdsa_p256.der | Bin 0 -> 138 bytes .../keys/dummy/unlock_ecdsa_p256.pub.der | Bin 0 -> 91 bytes 9 files changed, 30 insertions(+) create mode 100644 sw/device/silicon_creator/lib/ownership/keys/dummy/BUILD create mode 100644 sw/device/silicon_creator/lib/ownership/keys/dummy/activate_ecdsa_p256.der create mode 100644 sw/device/silicon_creator/lib/ownership/keys/dummy/activate_ecdsa_p256.pub.der create mode 100644 sw/device/silicon_creator/lib/ownership/keys/dummy/app_prod_rsa_3072_exp_f4.der create mode 100644 sw/device/silicon_creator/lib/ownership/keys/dummy/app_prod_rsa_3072_exp_f4.pub.der create mode 100644 sw/device/silicon_creator/lib/ownership/keys/dummy/owner_ecdsa_p256.der create mode 100644 sw/device/silicon_creator/lib/ownership/keys/dummy/owner_ecdsa_p256.pub.der create mode 100644 sw/device/silicon_creator/lib/ownership/keys/dummy/unlock_ecdsa_p256.der create mode 100644 sw/device/silicon_creator/lib/ownership/keys/dummy/unlock_ecdsa_p256.pub.der diff --git a/sw/device/silicon_creator/lib/ownership/keys/dummy/BUILD b/sw/device/silicon_creator/lib/ownership/keys/dummy/BUILD new file mode 100644 index 0000000000000..c143892271503 --- /dev/null +++ b/sw/device/silicon_creator/lib/ownership/keys/dummy/BUILD @@ -0,0 +1,30 @@ +# Copyright lowRISC contributors (OpenTitan project). +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 + +package(default_visibility = ["//visibility:public"]) + +filegroup( + name = "owner_key", + srcs = ["owner_ecdsa_p256.der"], +) + +filegroup( + name = "activate_key", + srcs = ["activate_ecdsa_p256.der"], +) + +filegroup( + name = "unlock_key", + srcs = ["unlock_ecdsa_p256.der"], +) + +filegroup( + name = "app_prod", + srcs = ["app_prod_rsa_3072_exp_f4.der"], +) + +filegroup( + name = "app_prod_pub", + srcs = ["app_prod_rsa_3072_exp_f4.pub.der"], +) diff --git a/sw/device/silicon_creator/lib/ownership/keys/dummy/activate_ecdsa_p256.der b/sw/device/silicon_creator/lib/ownership/keys/dummy/activate_ecdsa_p256.der new file mode 100644 index 0000000000000000000000000000000000000000..c0b4b4bd7472f97057ed9c41c0690e1ffbfbd9d9 GIT binary patch literal 138 zcmXqLY-eI*Fc4;A*J|@PXUoLM#sOw9GqSVf8e}suGO{QHf3Ml_vCqJAiI!K_%FsW1 zCpg_rc6ZZq5BR)VonxO<=t37}CkB?{ce?QymtxK3nhO9#ZhWRzsj*$gwQr4XQ{}NMXC6JsvhRKq|4@1Mu0jB)yEl*k literal 0 HcmV?d00001 diff --git a/sw/device/silicon_creator/lib/ownership/keys/dummy/activate_ecdsa_p256.pub.der b/sw/device/silicon_creator/lib/ownership/keys/dummy/activate_ecdsa_p256.pub.der new file mode 100644 index 0000000000000000000000000000000000000000..02b30b3a5fa1e7027946453e9ffc8981a6aac2f3 GIT binary patch literal 91 zcmXqrG!SNE*J|@PXUoLM#sOw9GqN)~F|ZWB(=~PSjb?nA>ad{o*?J%MxFX@^U+K>0 u^jR7wX`g)M)cN>#<1@8NjqNh7eQR`^Dvw<`^XNgAefOLAhsv{e6#@WwE+vcr literal 0 HcmV?d00001 diff --git a/sw/device/silicon_creator/lib/ownership/keys/dummy/app_prod_rsa_3072_exp_f4.der b/sw/device/silicon_creator/lib/ownership/keys/dummy/app_prod_rsa_3072_exp_f4.der new file mode 100644 index 0000000000000000000000000000000000000000..a41d54d309f0ffb238dbdd2ec47578cf94f419c2 GIT binary patch literal 1793 zcmV+c2mbglf(HEp0RS)!1_>&LNQUt=P-f>;{pKy0)hd70MSXoTiD$E zrB?!jrV7$K;JC*BDwCu3u9rrXGWh4y%Yn-3K>@z2ZfR0G&r40l`^&E2sq(ngQvki8 zyEdJ37?_3E#iHhA^6h!Kul@8pvXClv0HxOYLlq7xZ z#HC3MUhH+IxgUb8LpEXRhxFCn*(R4MJiVwDBtJQIcVcv?yIx9Y{*j!j@)gJYClj`nj2aAYcBi$9QEQuD0Ls zSC(~|?x9-lC5JPvMYQ6#9ejvcjzsZ^ibqDSRD}dKkF?VtFxj+InqS<(#_0~F9<5-F zR!Jk1Zt43^mC=CBQvw44009Dm0e}M%bQUKL$6lTCS%6%0O^~$ZrsX#omkVVH5--(T zfzDv>Ta@gxq0EyTGwF>FZ8lK-7Aq)r@)qsU+Q;U*RPQZ)`F7hkN|Vq`lOHrW8;?11 zh8*ZpNr;AgReB`Wr!*S!UQA+Q$LQ|>Gv$%CbOFwSpET7R8}Jh0$FY@nXJtKmH5ua} zK`?$UF^^Uq2;0RUJhqdb?&n}L5Vuj8=)G0*%Rle6EYA_;fHwniWtr~yZ2INvCB$yP z!LJtdk)NcgN%*Iss8#6YFEToDLeAXD3YC5&hVf4P(k+e|RN!qRG510fmEAUH;bqq= z15O&I!g+xR;OnZX9?>&)oqJ+1b5}ive7xA_)=7V8E)jP2JcN)^)y~C{rHI9Pa!;bx z@bwV{kJfobR1y%H#)3pLph@>;=;ot-f6oP?Z#SZR@8ZI`{_mn1uLC?Na}%%bi>F8! zui_rs;?Q-QT`Gv~gHhOycP~*hC8bU4>z(Ou9Sdk)1o_YVa1!jORAB;v!2s3+GMs#X zdXOTObJyhoo?spe-oOO=b})k4qB4m3r^yh2ft6hPjj$9z#oaGSNG}A7+v5)a2LVx( z-6_m*Y+T0YtX9+Wk8CUVHsv(j?`1P2DlTcT6HboU+W&ufc%Y)OB9=iXd>#dDo~|Ij zP}(r3x5?t4%56JE$#hz{8~41I8evolPGTdmuWJVJ!tL?o;wwA3p4QYViEz0shg~Bu zHK~lsC&KAJOw^HYlpx@!V~!2tSMMqI{j{QGDEvFa~`UCV@; zHfNwwjr|jb9-QtzVI_Vs@-GMaLc22B-BbRq$z?`^R)jo#>1<|-@KKWdeY4=6gj=jH zkzZ@WhE7Xx`L9rTds?A^1%W9--!3IJ)!8%_vqE5|k@@wVZG&nXw?z^KSZTXxV>IBt zs8M9l*{z~onYJE_LF}Ly*`C5b?K=BtfAYtF#>_X0NWb#SQA_15$v!ZB85NZ4^J52@WlaJvDrG3rshhj(wzyGACmP3-bIL2$!&BM5E3Fav*q%_TWnjdmFr`FuOG zZ@1eb+YG=%#d2OTY&HMzH@e)nssmKeo#II(IIn5dwk04+%xc z(nq4<@cmD@UUUs6xP?9KmUu3FUhJruVDs7iq<4Cj!scez1J*h`j51nPpAW%mc@O3+rZAb7osgc}iS53gZz9XrD)Ileo`(MXA!@K#PMd55L# z(L+(B`~sc@X+Qz3%zfEE^Np#LBz^3}rAZB5>~*HOAA+nyHeu?A^wr+u?l_1wWPjsQ z$*{Y8oqs5)Br1rQcybrv1O+bG0bz%j1BAHkSPr*u?e=9KJ<*#YZHMJ6Zt}sT zUcmg&ng24dt9=o32S>|(3UO~^E{FDkAL7d{Rl?K_LSh)S@ha?5l3|OqC|y<^NJ`Se zmS$=^UDpx%xtV|HANW(SXfU0s{d60p;k;>Hq)$ literal 0 HcmV?d00001 diff --git a/sw/device/silicon_creator/lib/ownership/keys/dummy/owner_ecdsa_p256.der b/sw/device/silicon_creator/lib/ownership/keys/dummy/owner_ecdsa_p256.der new file mode 100644 index 0000000000000000000000000000000000000000..8134d7740a9273af219f2ecdb1430bb0c9ef3017 GIT binary patch literal 138 zcmXqLY-eI*Fc4;A*J|@PXUoLM#sOw9GqSVf8e}suGO{R0bhX^((s`Nq?{RO>!s#iR z8ZQGJ+@`AeD?a?mxG_X$-$EB=Ck7VHMWNa9j_NDB3La>$O74tga{iX#yWit7+c68q qwD_-&U(B6z+*U~OpkLH$i8s+JyjV*Gl#-aTk6mzg2-@NjR004=oHFbH literal 0 HcmV?d00001 diff --git a/sw/device/silicon_creator/lib/ownership/keys/dummy/owner_ecdsa_p256.pub.der b/sw/device/silicon_creator/lib/ownership/keys/dummy/owner_ecdsa_p256.pub.der new file mode 100644 index 0000000000000000000000000000000000000000..a199a167415e858b9d6183a4514fca3579b924a0 GIT binary patch literal 91 zcmXqrG!SNE*J|@PXUoLM#sOw9GqN)~F|cSZ3eA>xRA1Ru@IZT2a%Uuy^S2D&{T`Ru tj#)6K#eaSLV(y&dwnB;r{i0q=yop}n#ab$$l*E*M?1H;P&=!}V5&$pcAq4;c literal 0 HcmV?d00001 diff --git a/sw/device/silicon_creator/lib/ownership/keys/dummy/unlock_ecdsa_p256.der b/sw/device/silicon_creator/lib/ownership/keys/dummy/unlock_ecdsa_p256.der new file mode 100644 index 0000000000000000000000000000000000000000..a52254cb97137d9d51294d4a222b0be31684ec9a GIT binary patch literal 138 zcmV;50CoQ`frkPC05B5<2P%e0&OHJF1_&yKNX|V20S5$aFlzz<0R$k4Wyn6UJK(k{ z$#L6r5IjU6U(+<0Zm#R}ovbC67*uL={Grzwr1&SgQa5MRU7~SR3*Lu literal 0 HcmV?d00001 From 859eff7327a063d60382721e0b01496f7ccdbd81 Mon Sep 17 00:00:00 2001 From: Chris Frantz Date: Fri, 3 May 2024 10:49:33 -0700 Subject: [PATCH 3/4] [opentitanlib] Bindgen `rom_error_t` into Rust Signed-off-by: Chris Frantz (cherry picked from commit 5ecb3fb6c93cccae1605a83d81bea32ad7a941b3) (cherry picked from commit 3837b325d212cc9abb83a9f0f6748cbafb2e808b) --- sw/host/opentitanlib/BUILD | 3 ++ sw/host/opentitanlib/bindgen/BUILD | 46 ++++++++++++++++++++-- sw/host/opentitanlib/bindgen/lib.rs | 5 +++ sw/host/opentitanlib/bindgen/rom_error.rs | 7 ++++ sw/host/opentitanlib/src/chip/boot_svc.rs | 26 ++++++------ sw/host/opentitanlib/src/chip/mod.rs | 1 + sw/host/opentitanlib/src/chip/rom_error.rs | 7 ++++ sw/host/opentitanlib/src/ownership/misc.rs | 1 + 8 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 sw/host/opentitanlib/bindgen/rom_error.rs create mode 100644 sw/host/opentitanlib/src/chip/rom_error.rs diff --git a/sw/host/opentitanlib/BUILD b/sw/host/opentitanlib/BUILD index 847fa1f34bcbc..14893395888cd 100644 --- a/sw/host/opentitanlib/BUILD +++ b/sw/host/opentitanlib/BUILD @@ -82,6 +82,7 @@ rust_library( "src/chip/boot_log.rs", "src/chip/boot_svc.rs", "src/chip/helper.rs", + "src/chip/rom_error.rs", "src/chip/mod.rs", "src/console/mod.rs", "src/console/spi.rs", @@ -259,6 +260,7 @@ rust_library( "//third_party/openocd:jtag_cmsis_dap_adapter_cfg", "//util/openocd/target:lowrisc-earlgrey.cfg", "//util/openocd/target:lowrisc-earlgrey-lc.cfg", + "//sw/host/opentitanlib/bindgen:rom_error_enum", ], crate_features = [ "include_hyperdebug_firmware", @@ -275,6 +277,7 @@ rust_library( "i2c_target": "$(location :i2c_target)", "mem": "$(location :mem)", "pinmux_config": "$(location :pinmux_config)", + "rom_error_enum": "$(location //sw/host/opentitanlib/bindgen:rom_error_enum)", "spi_passthru": "$(location :spi_passthru)", "ottf": "$(location :ottf)", "hyperdebug_firmware": "$(location @hyperdebug_firmware//:hyperdebug/ec.bin)", diff --git a/sw/host/opentitanlib/bindgen/BUILD b/sw/host/opentitanlib/bindgen/BUILD index 199074cfc12a9..dab3e013f2bf5 100644 --- a/sw/host/opentitanlib/bindgen/BUILD +++ b/sw/host/opentitanlib/bindgen/BUILD @@ -2,8 +2,8 @@ # Licensed under the Apache License, Version 2.0, see LICENSE for details. # SPDX-License-Identifier: Apache-2.0 -load("@rules_rust//bindgen:defs.bzl", "rust_bindgen_library") -load("@rules_rust//rust:defs.bzl", "rust_library") +load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") +load("@rules_rust//bindgen:defs.bzl", "rust_bindgen", "rust_bindgen_library") package(default_visibility = ["//visibility:public"]) @@ -187,6 +187,43 @@ rust_bindgen_library( ], ) +# We generate the bindgen source for rom_error_t so we can post-process it +# with the :rom_error_enum rule and build a full enum from all known +# rom_error_t values. +rust_bindgen( + name = "rom_error", + bindgen_flags = [ + "--allowlist-type=rom_error_t", + ], + cc_lib = "//sw/device/silicon_creator/lib:error", + header = "//sw/device/silicon_creator/lib:error.h", +) + +# Generate a rust source file that contains the enum definition. This +# will get textual-included into opentitanlib's `chip::rom_error` module. +genrule( + name = "rom_error_enum", + srcs = [":rom_error"], + outs = ["rom_error_enum.rs"], + cmd = """ +cat >$@ <
>$@ + +cat >>$@ <