Skip to content

Commit 892920e

Browse files
committed
chore: document general and app attributes
1 parent ddf7462 commit 892920e

2 files changed

Lines changed: 93 additions & 94 deletions

File tree

tockloader-lib/src/attributes/app_attributes.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ impl AppAttributes {
4848
/// The function below is used to retrieve header and footer data
4949
///
5050
/// Starting from the 0x40000 address,
51-
/// using the 'parse_tbf_header_lengths' function, we read the very first 8 bytes to determine:
52-
///
51+
/// using the ['parse_tbf_header_lengths'] function, we read the very first 8 bytes to determine:
52+
///
5353
/// - tbf-version
5454
/// - header_size
5555
/// - total_size
@@ -109,10 +109,10 @@ impl AppAttributes {
109109
.map_err(TockloaderError::ParsingError)?;
110110

111111
let binary_end_offset = header.get_binary_end(); // the end of the header marks the beginning of the footer
112-
113-
/// 1. Calculate the total size of all footers by subtracting the binary's end offset (`binary_end_offset`) from the total application size (`total_size`).
114-
/// 2. Initialize the reading offset (`footer_offset`) to the start of the footers (right after the binary ends).
115-
/// 3. Loop until we've read all footers (when `footer_offset` reaches `total_size`):
112+
113+
// 1. Calculate the total size of all footers by subtracting the binary's end offset (`binary_end_offset`) from the total application size (`total_size`).
114+
// 2. Initialize the reading offset (`footer_offset`) to the start of the footers (right after the binary ends).
115+
// 3. Loop until we've read all footers (when `footer_offset` reaches `total_size`):
116116
let mut footers: Vec<TbfFooter> = vec![];
117117
let mut footers: Vec<TbfFooter> = vec![];
118118
let total_footers_size = total_size - binary_end_offset;
@@ -172,25 +172,25 @@ impl AppAttributes {
172172
let mut pkt = (appaddr as u32).to_le_bytes().to_vec(); // the tockloader protocol only supports up to 32 bytes, hence we converted acquiring 4 byte little endians.
173173
let length = (8_u16).to_le_bytes().to_vec(); // 2 byte little endians
174174
for i in length {
175-
pkt.push(i);
175+
pkt.push(i);
176176
}
177-
178-
/// This sends a `ReadRange` command to the microcontroller to read 8 bytes of memory starting at `appaddr`,
179-
/// and receives the result over the serial connection.
180-
///
181-
/// The microcontroller receives the command, checks the Command ID (`0x06` for `ReadRange`),
182-
/// then reads the 1-byte payload length and the actual payload:
183-
/// 4 bytes for the address (little-endian), and 2 bytes for the length to read (also little-endian).
184-
///
185-
/// The microcontroller processes incoming bytes using a state machine like this:
186-
///
187-
/// enum SerialState {
188-
/// WaitingForCommand, // Awaiting the 1-byte command ID
189-
/// WaitingForLength, // Awaiting the 1-byte payload length
190-
/// WaitingForPayload { command: u8, length: usize, buffer: Vec<u8> }, // Receiving payload
191-
/// }
177+
178+
// This sends a `ReadRange` command to the microcontroller to read 8 bytes of memory starting at `appaddr`,
179+
// and receives the result over the serial connection.
180+
//
181+
// The microcontroller receives the command, checks the Command ID (`0x06` for `ReadRange`),
182+
// then reads the 1-byte payload length and the actual payload:
183+
// 4 bytes for the address (little-endian), and 2 bytes for the length to read (also little-endian).
184+
//
185+
// The microcontroller processes incoming bytes using a state machine like this:
186+
//
187+
// enum SerialState {
188+
// WaitingForCommand, // Awaiting the 1-byte command ID
189+
// WaitingForLength, // Awaiting the 1-byte payload length
190+
// WaitingForPayload { command: u8, length: usize, buffer: Vec<u8> }, // Receiving payload
191+
// }
192192
let (_, appdata) = // the first element in the tuple be returned is raw data so we ignore it and take the second which is the app data we need
193-
issue_command(port, Command::ReadRange, pkt, true, 8, Response::ReadRange).await?;
193+
issue_command(port, Command::ReadRange, pkt, true, 8, Response::ReadRange).await?;
194194

195195
let tbf_version: u16;
196196
let header_size: u16;
@@ -208,19 +208,19 @@ impl AppAttributes {
208208
}
209209
_ => break,
210210
};
211-
211+
212212
let mut pkt = (appaddr as u32).to_le_bytes().to_vec();
213213
let length = (header_size).to_le_bytes().to_vec();
214214
for i in length {
215215
pkt.push(i);
216216
}
217-
/// This also sends a ReadRange command to fetch the full TBF header from the same app address (appaddr).
218-
///
219-
/// The first read (8 bytes) gave us only the header lengths and total size,
220-
/// but now that we know the full header size (header_size), we send a new request to retrieve the entire header.
221-
///
222-
/// The returned data (header_data) is a raw byte vector containing all fields of the TBF header,
223-
/// including version, flags, entry point, protected regions, package name, etc.
217+
// This also sends a ReadRange command to fetch the full TBF header from the same app address (appaddr).
218+
//
219+
// The first read (8 bytes) gave us only the header lengths and total size,
220+
// but now that we know the full header size (header_size), we send a new request to retrieve the entire header.
221+
//
222+
// The returned data (header_data) is a raw byte vector containing all fields of the TBF header,
223+
// including version, flags, entry point, protected regions, package name, etc.
224224
let (_, header_data) = issue_command(
225225
port,
226226
Command::ReadRange,
@@ -249,11 +249,11 @@ impl AppAttributes {
249249
pkt.push(i);
250250
}
251251

252-
/// Issue a ReadRange command over serial to fetch the next footer block.
253-
///
254-
/// The response contains raw bytes starting from footer_offset, with the desired length.
255-
///
256-
/// We ignore the first tuple element and keep only the returned footer bytes.
252+
// Issue a ReadRange command over serial to fetch the next footer block.
253+
//
254+
// The response contains raw bytes starting from footer_offset, with the desired length.
255+
//
256+
// We ignore the first tuple element and keep only the returned footer bytes.
257257
let (_, appfooter) = issue_command(
258258
port,
259259
Command::ReadRange,

tockloader-lib/src/attributes/system_attributes.rs

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ impl SystemAttributes {
9999
};
100100

101101
let step_option = decode_attribute(step);
102-
103-
/// If the attribute chunk was successfully decoded, assign its value to the corresponding field in `result`
104-
/// based on the index:
105-
/// 0 = board name, 1 = architecture, 2 = application start address (parsed from hex string), 3 = boot hash,
106-
/// _ = invalid or missing data is skipped.
102+
103+
// If the attribute chunk was successfully decoded, assign its value to the corresponding field in `result`
104+
// based on the index:
105+
// 0 = board name, 1 = architecture, 2 = application start address (parsed from hex string), 3 = boot hash,
106+
// _ = invalid or missing data is skipped.
107107
if let Some(decoded_attributes) = step_option {
108108
match index_data {
109109
0 => {
@@ -162,8 +162,8 @@ impl SystemAttributes {
162162
"No start address found.".to_owned(),
163163
))? - 100,
164164
&mut kernel_attr_binary,
165-
)
166-
.map_err(TockloaderError::ProbeRsReadError)?; // The address is calculated as `(appaddr - 100)`, where `appaddr` marks the beginning of application binaries in flash.
165+
)
166+
.map_err(TockloaderError::ProbeRsReadError)?; // The address is calculated as `(appaddr - 100)`, where `appaddr` marks the beginning of application binaries in flash.
167167

168168
// Parse fixed fields from footer block
169169
let sentinel = bytes_to_string(&kernel_attr_binary[96..100]); // Expect "TOCK" as usual
@@ -174,18 +174,17 @@ impl SystemAttributes {
174174

175175
let kernel_binary_start = LittleEndian::read_u32(&kernel_attr_binary[68..72]);
176176
let kernel_binary_len = LittleEndian::read_u32(&kernel_attr_binary[72..76]);
177-
/// This what it looks like:
178-
///
179-
/// - 0-79: is for padding, basically unused.
180-
/// - 80-84: app_mem_start (u32, little-endian)
181-
/// - 84-88: app_mem_len (u32)
182-
/// - 88-92: kernel_mem_start (u32)
183-
/// - 92-96: kernel_mem_len (u32)
184-
/// - 96-100: Sentinel ("TOCK" ASCII)
185-
/// - 95-96: Kernel version (u8)
186-
177+
// This what it looks like:
178+
//
179+
// - 0-79: is for padding, basically unused.
180+
// - 80-84: app_mem_start (u32, little-endian)
181+
// - 84-88: app_mem_len (u32)
182+
// - 88-92: kernel_mem_start (u32)
183+
// - 92-96: kernel_mem_len (u32)
184+
// - 96-100: Sentinel ("TOCK" ASCII)
185+
// - 95-96: Kernel version (u8)
187186
// Store parsed values in result struct
188-
result.sentinel = Some(sentinel);
187+
result.sentinel = Some(sentinel);
189188
result.kernel_version = Some(kernel_version);
190189
result.app_mem_start = Some(app_memory_start);
191190
result.app_mem_len = Some(app_memory_len);
@@ -227,18 +226,18 @@ impl SystemAttributes {
227226
pkt.push(i); // Merges the two, making them 6 bytes in the vector
228227
}
229228

230-
/// This sends a `ReadRange` command to the microcontroller to read 1024 bytes of memory starting at address `0x600`.
231-
///
232-
/// The microcontroller expects the command in this structure:
233-
/// - 1 byte: Command ID (`0x06` or `0x10` for `ReadRange`)
234-
/// - 1 byte: Payload length (always 6 bytes for this command)
235-
/// - 6 bytes payload:
236-
/// - 4 bytes: Start address (little-endian)
237-
/// - 2 bytes: Number of bytes to read (little-endian)
238-
///
239-
/// In this case, we’re reading system attributes stored in memory starting at `0x600`.
240-
///
241-
/// The 1024-byte response will contain multiple 64-byte attribute blocks, each of which may encode a (key, value) pair.
229+
// This sends a `ReadRange` command to the microcontroller to read 1024 bytes of memory starting at address `0x600`.
230+
//
231+
// The microcontroller expects the command in this structure:
232+
// - 1 byte: Command ID (`0x06` or `0x10` for `ReadRange`)
233+
// - 1 byte: Payload length (always 6 bytes for this command)
234+
// - 6 bytes payload:
235+
// - 4 bytes: Start address (little-endian)
236+
// - 2 bytes: Number of bytes to read (little-endian)
237+
//
238+
// In this case, we’re reading system attributes stored in memory starting at `0x600`.
239+
//
240+
// The 1024-byte response will contain multiple 64-byte attribute blocks, each of which may encode a (key, value) pair.
242241
let (_, buf) = issue_command(
243242
port,
244243
Command::ReadRange, // Read memory (e.g. 0x10)
@@ -307,19 +306,19 @@ impl SystemAttributes {
307306
pkt.push(i); // Append length
308307
}
309308

310-
/// This sends a `ReadRange` command to read 8 bytes from address `0x40E`,
311-
/// which stores the bootloader version string (UTF-8 encoded).
312-
///
313-
/// The serial protocol proceeds as follows:
314-
/// - 1 byte: Command ID
315-
/// - 1 byte: Payload length = 6
316-
/// - Payload =
317-
/// - 4 bytes: Address (0x40E, little-endian)
318-
/// - 2 bytes: Length (`8`, little-endian)
319-
///
320-
/// The response is expected to be a UTF-8 encoded string, often null-terminated.
321-
///
322-
/// We decode the returned bytes into a `String`, trimming any null characters from the end.
309+
// This sends a `ReadRange` command to read 8 bytes from address `0x40E`,
310+
// which stores the bootloader version string (UTF-8 encoded).
311+
//
312+
// The serial protocol proceeds as follows:
313+
// - 1 byte: Command ID
314+
// - 1 byte: Payload length = 6
315+
// - Payload =
316+
// - 4 bytes: Address (0x40E, little-endian)
317+
// - 2 bytes: Length (`8`, little-endian)
318+
//
319+
// The response is expected to be a UTF-8 encoded string, often null-terminated.
320+
//
321+
// We decode the returned bytes into a `String`, trimming any null characters from the end.
323322
let (_, buf) =
324323
issue_command(port, Command::ReadRange, pkt, true, 8, Response::ReadRange).await?;
325324

@@ -344,21 +343,21 @@ impl SystemAttributes {
344343
pkt.push(i); // Append length
345344
}
346345

347-
/// This sends a `ReadRange` command to read the last 100 bytes before the start of the application space,
348-
/// which usually contains the kernel’s footer region.
349-
///
350-
/// The address is calculated as `(appaddr - 100)`, where `appaddr` marks the beginning of application binaries in flash.
351-
///
352-
/// The request structure is:
353-
/// - 1 byte: Command ID (`0x06`)
354-
/// - 1 byte: Payload length = 6
355-
/// - Payload =
356-
/// - 4 bytes: Address (`appaddr - 100`, little-endian)
357-
/// - 2 bytes: Length (`100`, little-endian)
358-
///
359-
/// The microcontroller responds with 100 bytes from that memory region.
360-
/// These bytes might contain metadata or checksums relevant to the kernel image.
361-
let (_, kernel_attr_binary) = issue_command(
346+
// This sends a `ReadRange` command to read the last 100 bytes before the start of the application space,
347+
// which usually contains the kernel’s footer region.
348+
//
349+
// The address is calculated as `(appaddr - 100)`, where `appaddr` marks the beginning of application binaries in flash.
350+
//
351+
// The request structure is:
352+
// - 1 byte: Command ID (`0x06`)
353+
// - 1 byte: Payload length = 6
354+
// - Payload =
355+
// - 4 bytes: Address (`appaddr - 100`, little-endian)
356+
// - 2 bytes: Length (`100`, little-endian)
357+
//
358+
// The microcontroller responds with 100 bytes from that memory region.
359+
// These bytes might contain metadata or checksums relevant to the kernel image.
360+
let (_, kernel_attr_binary) = issue_command(
362361
port,
363362
Command::ReadRange,
364363
pkt,

0 commit comments

Comments
 (0)