commit d305ba7f2d5ae524fd71365852fb5196e98fdbaf (tree)
parent 3d5ff91441040d11cca814f298231a501700da9e
Author: Andrew Kelley <andrew@ziglang.org>
Date: Tue, 7 Sep 2021 15:12:00 -0400
Merge pull request #9636 from ehaas/hexfiles
stdlib: Add Intel HEX support to InstallRawStep
Diffstat:
5 files changed, 414 insertions(+), 6 deletions(-)
diff --git a/lib/std/build.zig b/lib/std/build.zig
@@ -989,10 +989,15 @@ pub const Builder = struct {
self.getInstallStep().dependOn(&self.addInstallFileWithDir(.{ .path = src_path }, .lib, dest_rel_path).step);
}
+ /// Output format (BIN vs Intel HEX) determined by filename
pub fn installRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) void {
self.getInstallStep().dependOn(&self.addInstallRaw(artifact, dest_filename).step);
}
+ pub fn installRawWithFormat(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, format: InstallRawStep.RawFormat) void {
+ self.getInstallStep().dependOn(&self.addInstallRawWithFormat(artifact, dest_filename, format).step);
+ }
+
///`dest_rel_path` is relative to install prefix path
pub fn addInstallFile(self: *Builder, source: FileSource, dest_rel_path: []const u8) *InstallFileStep {
return self.addInstallFileWithDir(source.dupe(self), .prefix, dest_rel_path);
@@ -1009,7 +1014,11 @@ pub const Builder = struct {
}
pub fn addInstallRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) *InstallRawStep {
- return InstallRawStep.create(self, artifact, dest_filename);
+ return InstallRawStep.create(self, artifact, dest_filename, null);
+ }
+
+ pub fn addInstallRawWithFormat(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, format: InstallRawStep.RawFormat) *InstallRawStep {
+ return InstallRawStep.create(self, artifact, dest_filename, format);
}
pub fn addInstallFileWithDir(
@@ -1709,6 +1718,10 @@ pub const LibExeObjStep = struct {
self.builder.installRaw(self, dest_filename);
}
+ pub fn installRawWithFormat(self: *LibExeObjStep, dest_filename: []const u8, format: InstallRawStep.RawFormat) void {
+ self.builder.installRawWithFormat(self, dest_filename, format);
+ }
+
/// Creates a `RunStep` with an executable built with `addExecutable`.
/// Add command line arguments with `addArg`.
pub fn run(exe: *LibExeObjStep) *RunStep {
diff --git a/lib/std/build/InstallRawStep.zig b/lib/std/build/InstallRawStep.zig
@@ -159,7 +159,147 @@ fn writeBinaryElfSection(elf_file: File, out_file: File, section: *BinaryElfSect
});
}
-fn emitRaw(allocator: *Allocator, elf_path: []const u8, raw_path: []const u8) !void {
+const HexWriter = struct {
+ prev_addr: ?u32 = null,
+ out_file: File,
+
+ /// Max data bytes per line of output
+ const MAX_PAYLOAD_LEN: u8 = 16;
+
+ fn addressParts(address: u16) [2]u8 {
+ const msb = @truncate(u8, address >> 8);
+ const lsb = @truncate(u8, address);
+ return [2]u8{ msb, lsb };
+ }
+
+ const Record = struct {
+ const Type = enum(u8) {
+ Data = 0,
+ EOF = 1,
+ ExtendedSegmentAddress = 2,
+ ExtendedLinearAddress = 4,
+ };
+
+ address: u16,
+ payload: union(Type) {
+ Data: []const u8,
+ EOF: void,
+ ExtendedSegmentAddress: [2]u8,
+ ExtendedLinearAddress: [2]u8,
+ },
+
+ fn EOF() Record {
+ return Record{
+ .address = 0,
+ .payload = .EOF,
+ };
+ }
+
+ fn Data(address: u32, data: []const u8) Record {
+ return Record{
+ .address = @intCast(u16, address % 0x10000),
+ .payload = .{ .Data = data },
+ };
+ }
+
+ fn Address(address: u32) Record {
+ std.debug.assert(address > 0xFFFF);
+ const segment = @intCast(u16, address / 0x10000);
+ if (address > 0xFFFFF) {
+ return Record{
+ .address = 0,
+ .payload = .{ .ExtendedLinearAddress = addressParts(segment) },
+ };
+ } else {
+ return Record{
+ .address = 0,
+ .payload = .{ .ExtendedSegmentAddress = addressParts(segment << 12) },
+ };
+ }
+ }
+
+ fn getPayloadBytes(self: Record) []const u8 {
+ return switch (self.payload) {
+ .Data => |d| d,
+ .EOF => @as([]const u8, &.{}),
+ .ExtendedSegmentAddress, .ExtendedLinearAddress => |*seg| seg,
+ };
+ }
+
+ fn checksum(self: Record) u8 {
+ const payload_bytes = self.getPayloadBytes();
+
+ var sum: u8 = @intCast(u8, payload_bytes.len);
+ const parts = addressParts(self.address);
+ sum +%= parts[0];
+ sum +%= parts[1];
+ sum +%= @enumToInt(self.payload);
+ for (payload_bytes) |byte| {
+ sum +%= byte;
+ }
+ return (sum ^ 0xFF) +% 1;
+ }
+
+ fn write(self: Record, file: File) File.WriteError!void {
+ const linesep = "\r\n";
+ // colon, (length, address, type, payload, checksum) as hex, CRLF
+ const BUFSIZE = 1 + (1 + 2 + 1 + MAX_PAYLOAD_LEN + 1) * 2 + linesep.len;
+ var outbuf: [BUFSIZE]u8 = undefined;
+ const payload_bytes = self.getPayloadBytes();
+ std.debug.assert(payload_bytes.len <= MAX_PAYLOAD_LEN);
+
+ const line = try std.fmt.bufPrint(&outbuf, ":{0X:0>2}{1X:0>4}{2X:0>2}{3s}{4X:0>2}" ++ linesep, .{
+ @intCast(u8, payload_bytes.len),
+ self.address,
+ @enumToInt(self.payload),
+ std.fmt.fmtSliceHexUpper(payload_bytes),
+ self.checksum(),
+ });
+ try file.writeAll(line);
+ }
+ };
+
+ pub fn writeSegment(self: *HexWriter, segment: *const BinaryElfSegment, elf_file: File) !void {
+ var buf: [MAX_PAYLOAD_LEN]u8 = undefined;
+ var bytes_read: usize = 0;
+ while (bytes_read < segment.fileSize) {
+ const row_address = @intCast(u32, segment.physicalAddress + bytes_read);
+
+ const remaining = segment.fileSize - bytes_read;
+ const to_read = @minimum(remaining, MAX_PAYLOAD_LEN);
+ const did_read = try elf_file.preadAll(buf[0..to_read], segment.elfOffset + bytes_read);
+ if (did_read < to_read) return error.UnexpectedEOF;
+
+ try self.writeDataRow(row_address, buf[0..did_read]);
+
+ bytes_read += did_read;
+ }
+ }
+
+ fn writeDataRow(self: *HexWriter, address: u32, data: []const u8) File.WriteError!void {
+ const record = Record.Data(address, data);
+ if (address > 0xFFFF and (self.prev_addr == null or record.address != self.prev_addr.?)) {
+ try Record.Address(address).write(self.out_file);
+ }
+ try record.write(self.out_file);
+ self.prev_addr = @intCast(u32, record.address + data.len);
+ }
+
+ fn writeEOF(self: HexWriter) File.WriteError!void {
+ try Record.EOF().write(self.out_file);
+ }
+};
+
+fn containsValidAddressRange(segments: []*BinaryElfSegment) bool {
+ const max_address = std.math.maxInt(u32);
+ for (segments) |segment| {
+ if (segment.fileSize > max_address or
+ segment.physicalAddress > max_address - segment.fileSize) return false;
+ }
+ return true;
+}
+
+fn emitRaw(allocator: *Allocator, elf_path: []const u8, raw_path: []const u8, format: RawFormat) !void {
var elf_file = try fs.cwd().openFile(elf_path, .{});
defer elf_file.close();
@@ -169,8 +309,26 @@ fn emitRaw(allocator: *Allocator, elf_path: []const u8, raw_path: []const u8) !v
var binary_elf_output = try BinaryElfOutput.parse(allocator, elf_file);
defer binary_elf_output.deinit();
- for (binary_elf_output.sections.items) |section| {
- try writeBinaryElfSection(elf_file, out_file, section);
+ switch (format) {
+ .bin => {
+ for (binary_elf_output.sections.items) |section| {
+ try writeBinaryElfSection(elf_file, out_file, section);
+ }
+ },
+ .hex => {
+ if (binary_elf_output.segments.items.len == 0) return;
+ if (!containsValidAddressRange(binary_elf_output.segments.items)) {
+ return error.InvalidHexfileAddressRange;
+ }
+
+ var hex_writer = HexWriter{ .out_file = out_file };
+ for (binary_elf_output.sections.items) |section| {
+ if (section.segment) |segment| {
+ try hex_writer.writeSegment(segment, elf_file);
+ }
+ }
+ try hex_writer.writeEOF();
+ },
}
}
@@ -178,13 +336,27 @@ const InstallRawStep = @This();
pub const base_id = .install_raw;
+pub const RawFormat = enum {
+ bin,
+ hex,
+};
+
step: Step,
builder: *Builder,
artifact: *LibExeObjStep,
dest_dir: InstallDir,
dest_filename: []const u8,
+format: RawFormat,
+output_file: std.build.GeneratedFile,
+
+fn detectFormat(filename: []const u8) RawFormat {
+ if (std.mem.endsWith(u8, filename, ".hex") or std.mem.endsWith(u8, filename, ".ihex")) {
+ return .hex;
+ }
+ return .bin;
+}
-pub fn create(builder: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) *InstallRawStep {
+pub fn create(builder: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, format: ?RawFormat) *InstallRawStep {
const self = builder.allocator.create(InstallRawStep) catch unreachable;
self.* = InstallRawStep{
.step = Step.init(.install_raw, builder.fmt("install raw binary {s}", .{artifact.step.name}), builder.allocator, make),
@@ -197,6 +369,8 @@ pub fn create(builder: *Builder, artifact: *LibExeObjStep, dest_filename: []cons
.lib => unreachable,
},
.dest_filename = dest_filename,
+ .format = format orelse detectFormat(dest_filename),
+ .output_file = std.build.GeneratedFile{ .step = &self.step },
};
self.step.dependOn(&artifact.step);
@@ -204,6 +378,10 @@ pub fn create(builder: *Builder, artifact: *LibExeObjStep, dest_filename: []cons
return self;
}
+pub fn getOutputSource(self: *const InstallRawStep) std.build.FileSource {
+ return std.build.FileSource{ .generated = &self.output_file };
+}
+
fn make(step: *Step) !void {
const self = @fieldParentPtr(InstallRawStep, "step", step);
const builder = self.builder;
@@ -217,9 +395,49 @@ fn make(step: *Step) !void {
const full_dest_path = builder.getInstallPath(self.dest_dir, self.dest_filename);
fs.cwd().makePath(builder.getInstallPath(self.dest_dir, "")) catch unreachable;
- try emitRaw(builder.allocator, full_src_path, full_dest_path);
+ try emitRaw(builder.allocator, full_src_path, full_dest_path, self.format);
+ self.output_file.path = full_dest_path;
}
test {
std.testing.refAllDecls(InstallRawStep);
}
+
+test "Detect format from filename" {
+ try std.testing.expectEqual(RawFormat.hex, detectFormat("foo.hex"));
+ try std.testing.expectEqual(RawFormat.hex, detectFormat("foo.ihex"));
+ try std.testing.expectEqual(RawFormat.bin, detectFormat("foo.bin"));
+ try std.testing.expectEqual(RawFormat.bin, detectFormat("foo.bar"));
+ try std.testing.expectEqual(RawFormat.bin, detectFormat("a"));
+}
+
+test "containsValidAddressRange" {
+ var segment = BinaryElfSegment{
+ .physicalAddress = 0,
+ .virtualAddress = 0,
+ .elfOffset = 0,
+ .binaryOffset = 0,
+ .fileSize = 0,
+ .firstSection = null,
+ };
+ var buf: [1]*BinaryElfSegment = .{&segment};
+
+ // segment too big
+ segment.fileSize = std.math.maxInt(u32) + 1;
+ try std.testing.expect(!containsValidAddressRange(&buf));
+
+ // start address too big
+ segment.physicalAddress = std.math.maxInt(u32) + 1;
+ segment.fileSize = 2;
+ try std.testing.expect(!containsValidAddressRange(&buf));
+
+ // max address too big
+ segment.physicalAddress = std.math.maxInt(u32) - 1;
+ segment.fileSize = 2;
+ try std.testing.expect(!containsValidAddressRange(&buf));
+
+ // is ok
+ segment.physicalAddress = std.math.maxInt(u32) - 1;
+ segment.fileSize = 1;
+ try std.testing.expect(containsValidAddressRange(&buf));
+}
diff --git a/test/standalone.zig b/test/standalone.zig
@@ -27,6 +27,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
cases.addBuildFile("test/standalone/brace_expansion/build.zig", .{});
cases.addBuildFile("test/standalone/empty_env/build.zig", .{});
cases.addBuildFile("test/standalone/issue_7030/build.zig", .{});
+ cases.addBuildFile("test/standalone/install_raw_hex/build.zig", .{});
if (std.Target.current.os.tag != .wasi) {
cases.addBuildFile("test/standalone/load_dynamic_library/build.zig", .{});
}
diff --git a/test/standalone/install_raw_hex/build.zig b/test/standalone/install_raw_hex/build.zig
@@ -0,0 +1,173 @@
+const Builder = @import("std").build.Builder;
+const builtin = @import("builtin");
+const std = @import("std");
+const CheckFileStep = std.build.CheckFileStep;
+
+pub fn build(b: *Builder) void {
+ const target = .{
+ .cpu_arch = .thumb,
+ .cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m4 },
+ .os_tag = .freestanding,
+ .abi = .gnueabihf,
+ };
+
+ const mode = b.standardReleaseOptions();
+
+ const elf = b.addExecutable("zig-nrf52-blink.elf", "main.zig");
+ elf.setTarget(target);
+ elf.setBuildMode(mode);
+
+ const test_step = b.step("test", "Test the program");
+ b.default_step.dependOn(test_step);
+
+ const hex_step = b.addInstallRaw(elf, "hello.hex");
+ test_step.dependOn(&hex_step.step);
+
+ const explicit_format_hex_step = b.addInstallRawWithFormat(elf, "hello.foo", .hex);
+ test_step.dependOn(&explicit_format_hex_step.step);
+
+ const expected_hex = &[_][]const u8{
+ ":020000021000EC",
+ ":1000D400D001010001000000D20101000100000074",
+ ":1000E40028020100010000002402010001000000B8",
+ ":1000F4003A02010001000000E202010001000000D8",
+ ":100104000C03010001000000A50202000000000031",
+ ":1001140000000000000000000000000000000000DB",
+ ":1001240000000000000000000000000000000000CB",
+ ":1001340000000000000000000000000000000000BB",
+ ":10014400AF02020098020100090000004D02010004",
+ ":100154000900000001000000000000000000000091",
+ ":100164000004000100C0800000020000080000003C",
+ ":100174000000000000000000000000001E0000005D",
+ ":1001840048010100000040888080FF000A14002913",
+ ":1001940000C0B00000020090080000000000000051",
+ ":1001A40000000000000000001E000000000000002D",
+ ":1001B400000000000000000000000000000000003B",
+ ":1001C400000000000000000000000000000000002B",
+ ":1001D400000000000000000000000000000000001B",
+ ":1001E400000000000000000000000000000000000B",
+ ":1001F4000000000000000000000000008702010071",
+ ":1002040010000000200201002C0000006B0201001D",
+ ":100214001B000000570201001300000072656D61AD",
+ ":10022400696E646572206469766973696F6E2062B1",
+ ":1002340079207A65726F206F72206E6567617469C8",
+ ":1002440076652076616C756500636F727465782DD0",
+ ":100254006D3400696E646578206F7574206F662054",
+ ":10026400626F756E647300696E746567657220638E",
+ ":10027400617374207472756E63617465642062695D",
+ ":100284007473006469766973696F6E206279207A89",
+ ":1002940065726F00636F727465785F6D340000007F",
+ ":1002A40081B00091FFE700BEFDE7D0B502AF90B08A",
+ ":1002B4000391029007A800F029F803990020069002",
+ ":1002C40048680490FFE704990698019088420FD289",
+ ":1002D400FFE7019903980068405C07F8310C17F8B0",
+ ":1002E400311C07A800F021F8019801300690EAE7D4",
+ ":1002F400029807A9B1E80C50A0E80C5091E81C50F2",
+ ":1003040080E81C5010B0D0BDFFE7FEE7D0B502AFC7",
+ ":1003140040F2DC11C0F20101B1E80C50A0E80C502D",
+ ":1003240091E81C5080E81C50D0BD80B56F4688B061",
+ ":1003340006906FF35F2127F80A1C37F80A0C049023",
+ ":10034400012038B9FFE740F20020C0F2010000218B",
+ ":10035400FFF7A6FF0498C0F3431027F8020C37F800",
+ ":100364000A0C0390002038B9FFE7039800F01F003F",
+ ":100374000290012038B914E040F20820C0F20100D4",
+ ":100384000021FFF78DFF029800F01F0007F8030C0F",
+ ":100394000698009037F8020C0146019109280ED303",
+ ":1003A40006E040F21020C0F201000021FFF778FFC0",
+ ":1003B40040F21820C0F201000021FFF771FF0099FC",
+ ":1003C400019A51F8220017F803CC012303FA0CF325",
+ ":1003D400184341F8220008B080BD81B000F03F000E",
+ ":1003E4008DF802009DF80200103000F03F00022852",
+ ":1003F40004D3FFE700208DF8030003E001208DF80B",
+ ":100404000300FFE79DF8030001B070470A000000F5",
+ ":1004140012000000020071001200000066000000DB",
+ ":1004240003007D0C06000000000000000001110123",
+ ":10043400250E1305030E10171B0EB44219110112D9",
+ ":0604440006000002340076",
+ ":020000021000EC",
+ ":1000D400D001010001000000D20101000100000074",
+ ":1000E40028020100010000002402010001000000B8",
+ ":1000F4003A02010001000000E202010001000000D8",
+ ":100104000C03010001000000A50202000000000031",
+ ":1001140000000000000000000000000000000000DB",
+ ":1001240000000000000000000000000000000000CB",
+ ":1001340000000000000000000000000000000000BB",
+ ":10014400AF02020098020100090000004D02010004",
+ ":100154000900000001000000000000000000000091",
+ ":100164000004000100C0800000020000080000003C",
+ ":100174000000000000000000000000001E0000005D",
+ ":1001840048010100000040888080FF000A14002913",
+ ":1001940000C0B00000020090080000000000000051",
+ ":1001A40000000000000000001E000000000000002D",
+ ":1001B400000000000000000000000000000000003B",
+ ":1001C400000000000000000000000000000000002B",
+ ":1001D400000000000000000000000000000000001B",
+ ":1001E400000000000000000000000000000000000B",
+ ":1001F4000000000000000000000000008702010071",
+ ":1002040010000000200201002C0000006B0201001D",
+ ":100214001B000000570201001300000072656D61AD",
+ ":10022400696E646572206469766973696F6E2062B1",
+ ":1002340079207A65726F206F72206E6567617469C8",
+ ":1002440076652076616C756500636F727465782DD0",
+ ":100254006D3400696E646578206F7574206F662054",
+ ":10026400626F756E647300696E746567657220638E",
+ ":10027400617374207472756E63617465642062695D",
+ ":100284007473006469766973696F6E206279207A89",
+ ":1002940065726F00636F727465785F6D340000007F",
+ ":1002A40081B00091FFE700BEFDE7D0B502AF90B08A",
+ ":1002B4000391029007A800F029F803990020069002",
+ ":1002C40048680490FFE704990698019088420FD289",
+ ":1002D400FFE7019903980068405C07F8310C17F8B0",
+ ":1002E400311C07A800F021F8019801300690EAE7D4",
+ ":1002F400029807A9B1E80C50A0E80C5091E81C50F2",
+ ":1003040080E81C5010B0D0BDFFE7FEE7D0B502AFC7",
+ ":1003140040F2DC11C0F20101B1E80C50A0E80C502D",
+ ":1003240091E81C5080E81C50D0BD80B56F4688B061",
+ ":1003340006906FF35F2127F80A1C37F80A0C049023",
+ ":10034400012038B9FFE740F20020C0F2010000218B",
+ ":10035400FFF7A6FF0498C0F3431027F8020C37F800",
+ ":100364000A0C0390002038B9FFE7039800F01F003F",
+ ":100374000290012038B914E040F20820C0F20100D4",
+ ":100384000021FFF78DFF029800F01F0007F8030C0F",
+ ":100394000698009037F8020C0146019109280ED303",
+ ":1003A40006E040F21020C0F201000021FFF778FFC0",
+ ":1003B40040F21820C0F201000021FFF771FF0099FC",
+ ":1003C400019A51F8220017F803CC012303FA0CF325",
+ ":1003D400184341F8220008B080BD81B000F03F000E",
+ ":1003E4008DF802009DF80200103000F03F00022852",
+ ":1003F40004D3FFE700208DF8030003E001208DF80B",
+ ":100404000300FFE79DF8030001B070470A000000F5",
+ ":1004140012000000020071001200000066000000DB",
+ ":1004240003007D0C06000000000000000001110123",
+ ":10043400250E1305030E10171B0EB44219110112D9",
+ ":0604440006000002340076",
+ ":020000022000DC",
+ ":1002A40081B00091FFE700BEFDE7D0B502AF90B08A",
+ ":1002B4000391029007A800F029F803990020069002",
+ ":1002C40048680490FFE704990698019088420FD289",
+ ":1002D400FFE7019903980068405C07F8310C17F8B0",
+ ":1002E400311C07A800F021F8019801300690EAE7D4",
+ ":1002F400029807A9B1E80C50A0E80C5091E81C50F2",
+ ":1003040080E81C5010B0D0BDFFE7FEE7D0B502AFC7",
+ ":1003140040F2DC11C0F20101B1E80C50A0E80C502D",
+ ":1003240091E81C5080E81C50D0BD80B56F4688B061",
+ ":1003340006906FF35F2127F80A1C37F80A0C049023",
+ ":10034400012038B9FFE740F20020C0F2010000218B",
+ ":10035400FFF7A6FF0498C0F3431027F8020C37F800",
+ ":100364000A0C0390002038B9FFE7039800F01F003F",
+ ":100374000290012038B914E040F20820C0F20100D4",
+ ":100384000021FFF78DFF029800F01F0007F8030C0F",
+ ":100394000698009037F8020C0146019109280ED303",
+ ":1003A40006E040F21020C0F201000021FFF778FFC0",
+ ":1003B40040F21820C0F201000021FFF771FF0099FC",
+ ":1003C400019A51F8220017F803CC012303FA0CF325",
+ ":1003D400184341F8220008B080BD81B000F03F000E",
+ ":1003E4008DF802009DF80200103000F03F00022852",
+ ":1003F40004D3FFE700208DF8030003E001208DF80B",
+ ":0C0404000300FFE79DF8030001B0704703",
+ ":00000001FF",
+ };
+
+ test_step.dependOn(&CheckFileStep.create(b, hex_step.getOutputSource(), expected_hex).step);
+ test_step.dependOn(&CheckFileStep.create(b, explicit_format_hex_step.getOutputSource(), expected_hex).step);
+}
diff --git a/test/standalone/install_raw_hex/main.zig b/test/standalone/install_raw_hex/main.zig
@@ -0,0 +1,3 @@
+export fn _start() callconv(.C) noreturn {
+ while (true) {}
+}