1
0
Fork 0

Rename binary for clarity

This commit is contained in:
Shawn Nock 2020-01-13 13:38:14 -05:00
parent 0f510c4fc8
commit 7613724473
5 changed files with 1 additions and 143 deletions

View File

@ -3,7 +3,7 @@ const builtin = @import("builtin");
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("update-em-bootloader", "src/main.zig");
const exe = b.addExecutable("update-em-bootloader-feb2019", "src/main.zig");
exe.setBuildMode(builtin.Mode.ReleaseSmall);
exe.strip = true;

BIN
mtd-rw.ko

Binary file not shown.

View File

@ -1,69 +0,0 @@
const std = @import("std");
const os = std.os;
const mtd_rw_ko = @embedFile("mtd-rw.ko");
const InitModuleError = error {
ModuleSignatureMisformatted,
TimeoutResolvingSymbol,
BadAddress,
ModuleSigInvalid,
OutOfMemory,
PermissionDenied,
AlreadyLoaded,
BadParams,
InvalidModule,
};
const DeleteModuleError = error {
ModuleNotLive,
BadAddress,
ModuleNotFound,
PermissionDenied,
ModuleInUse,
};
pub fn load() !void {
try insmod(mtd_rw_ko, "i_want_a_brick=1");
}
pub fn unload() void {
rmmod("mtd_rw", os.O_NONBLOCK) catch |err| {
std.debug.warn("Failed to unload module: {}\n", .{err});
};
}
pub fn insmod(buf: []const u8, args: [*:0]const u8) !void {
const errno = os.linux.getErrno(
os.linux.syscall3(
os.linux.SYS_init_module, @ptrToInt(&buf[0]), buf.len, @ptrToInt(args)));
switch (errno) {
0 => return,
os.EEXIST => return, // It's not a failure if we have what we need
os.EBADMSG => return InitModuleError.ModuleSignatureMisformatted,
os.EBUSY => return InitModuleError.TimeoutResolvingSymbol,
os.EFAULT => return InitModuleError.BadAddress,
// os.ENOKEY => return InitModuleError.ModuleSigInvalid,
os.ENOMEM => return InitModuleError.OutOfMemory,
os.EPERM => return InitModuleError.PermissionDenied,
os.EINVAL => return InitModuleError.BadParams,
os.ENOEXEC => return InitModuleError.InvalidModule,
else => |err| return os.unexpectedErrno(err),
}
}
pub fn rmmod(mod_name: [*:0]const u8, flags: u32) !void {
const errno = os.linux.getErrno(
std.os.linux.syscall2(
std.os.linux.SYS_delete_module, @ptrToInt(mod_name), flags));
switch (errno) {
0 => return,
os.EBUSY => return DeleteModuleError.ModuleNotLive,
os.EFAULT => return DeleteModuleError.BadAddress,
os.ENOENT => return DeleteModuleError.ModuleNotFound,
os.EPERM => return DeleteModuleError.PermissionDenied,
os.EWOULDBLOCK => return DeleteModuleError.ModuleInUse,
else => |err| return os.unexpectedErrno(err),
}
}

Binary file not shown.

View File

@ -1,73 +0,0 @@
const std = @import("std");
const math = std.math;
const mtd_user = @cImport(@cInclude("mtd/mtd-user.h"));
const uboot = @embedFile("uboot-feb2019.bin");
const mtd_rw = @import("mtd-rw.zig");
const Allocator = std.heap.c_allocator;
fn mtd_info_get(name: []const u8, out: []u8) !void {
var path_buf: [64]u8 = undefined;
const path = try std.fmt.bufPrint(path_buf[0..], "{}/{}", .{"/sys/class/mtd/mtd0/", name});
var f = try std.fs.openFileAbsolute(path, .{.read = true});
defer f.close();
const num_read = try f.read(out);
out = out[0..num_read-1];
}
fn mtd_info_get_usize(name: []const u8) !usize {
var buf: [64]u8 = undefined;
var buf_sl = buf[0..];
try mtd_info_get(name, buf_sl);
return try std.fmt.parseUnsigned(usize, buf_sl, 10);
}
pub fn main() !u8 {
try mtd_rw.load();
defer mtd_rw.unload();
// Read and hash mtd0
const mtd0_size = try mtd_info_get_usize("size");
if (uboot.len > mtd0_size) {
std.debug.warn("Error: mtd0 isn\'t large enough to hold the new image ({} > {})\n",
.{uboot.len, mtd0_size});
return 1;
}
var current_uboot = try Allocator.alloc(u8, mtd0_size);
var f = try std.fs.openFileAbsolute("/dev/mtd0", .{.read = true});
defer f.close();
const num_read = try f.read(current_uboot[0..]);
if (num_read != mtd0_size) {
std.debug.warn("Failed to read /dev/mtd0", .{});
return 2;
}
var current_digest: [std.crypto.Md5.digest_length]u8 = undefined;
std.crypto.Md5.hash(current_uboot, current_digest[0..]);
// Compare against payload
var embedded_digest: [std.crypto.Md5.digest_length]u8 = undefined;
std.crypto.Md5.hash(uboot, embedded_digest[0..]);
if (std.mem.eql(u8, current_digest[0..], embedded_digest[0..])) {
std.debug.warn("U-Boot is up to date. Exiting.\n", .{});
return 0;
}
// Erase mtd0
const mtd0_erasesize = try mtd_info_get_usize("erasesize");
var erased = 0;
while (erased < mtd0_size) {
//erase page
erased += mtd0_erasesize;
}
// Write mtd0
// Verify mtd0
std.debug.warn("Done\n", .{});
return 0;
}