Skip to content

Commit

Permalink
Update code to be more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoLlobet committed Jan 11, 2024
1 parent dfee998 commit 5244508
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/boot/app.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn firmwareRestore() !firmware_update_outcome {
fn checkFirmwareCandidate() !update_phase {
_ = c.printf("Checking firmware image\n");

try firmware.checkFirmwareImage(); // If this fails, we can't continue.
try firmware.checkFirmwareImage(config.fw_file_name); // If this fails, we can't continue.

return update_phase.backup; // next phase
}
Expand Down
11 changes: 7 additions & 4 deletions src/boot/firmware.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ pub fn verifyFirmware(path: [*:0]const u8) !void {
/// Error Cases:
/// - If the file size is larger than the allowed firmware size, then `flash_firmware_size_error` is returned
/// - If the signature verification fails, the process will be aborted and `firmware_candidate_not_valid` is returned
pub fn checkFirmwareImage() !void {
const cand_len = try checkFirmwareCandidateSize(config.fw_file_name, null); // Either the file is not open-able or the size is too large
pub fn checkFirmwareImage(path: [*:0]const u8) !void {
const cand_len = try checkFirmwareCandidateSize(path, null); // Either the file is not open-able or the size is too large

if (verifyBackup(config.fw_file_name, cand_len)) |_| {
if (verifyBackup(path, cand_len)) |_| {
return firmware_error.firmware_already_in_system;
} else |err| {
if (err == firmware_error.hash_compare_mismatch) {
// Perform the signature verification
load_global_public_key();

verifyFirmware(config.fw_file_name) catch {
verifyFirmware(path) catch {
return firmware_error.firmware_candidate_not_valid;
};
}
Expand Down Expand Up @@ -215,6 +215,9 @@ pub export fn flash_area_read(fa: [*c]const flash_area, off: u32, dst: ?*anyopaq

fil.lseek(off) catch unreachable;
_ = fil.read(dst_slice) catch unreachable;
} else {
// Read from flash is easy using slice-math
@memcpy(dst_slice, fw[off..(off + len)]);
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub const config_local_error = error{
pub const config_error = (fatfs.frError || sha256.sha256_error || std.mem.Allocator.Error || pk.pk_error || mbedtls.mbedtls_error);

/// Const File block Size
const file_block_size: usize = 512;
pub const file_block_size: usize = 512;

/// Helper Getters
pub inline fn getHttpSigKey() []u8 {
Expand Down
10 changes: 2 additions & 8 deletions src/user.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ fn myUserTaskFunction(pvParameters: ?*anyopaque) callconv(.C) void {
_ = c.printf("Failure!!\n\r");
};

//board.watchdogFeed();
_ = config.open_config_file(config.config_file_name) catch {
_ = c.printf("Failure!!\n\r");
};
Expand Down Expand Up @@ -143,14 +142,9 @@ fn myUserTaskFunction(pvParameters: ?*anyopaque) callconv(.C) void {

fn downloadAndVerify() !bool {
// Download the firmware
try http.service.filedownload(config.getHttpFwUri(), config.fw_file_name, 512, 1024 * 1024);
try http.service.filedownload(config.getHttpFwUri(), config.fw_file_name, config.file_block_size, 1024 * 1024);

// Download the signature
//try http.service.filedownload(config.getHttpSigUri(), config.fw_sig_file_name, 512, 1024 * 1024);

try firmware.checkFirmwareImage();

//try config.verifyFirmwareSignature(config.fw_file_name, config.fw_sig_file_name, config.getHttpSigKey());
try firmware.checkFirmwareImage(config.fw_file_name);

return true;
}
Expand Down

0 comments on commit 5244508

Please sign in to comment.