-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzboot.zig
418 lines (361 loc) · 13.5 KB
/
zboot.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/// zboot
/// Author: @flyboy
///
const std = @import("std");
const mem = std.mem;
const json = std.json;
const Debug = false;
const KiB = 1024;
const ZC = @import("src/platform/sys.zig").zconfig;
const Part = @import("src/platform/fal/fal.zig").partition;
const OTA = @import("src/platform/ota/ota.zig");
const stm32zboot = @embedFile("zig-out/bin/stm32-zboot.bin");
const stm32app = @embedFile("zig-out/bin/stm32-app.bin");
const configjson = @embedFile("config.json");
pub var partition_num: u32 = 0;
pub var default_partition: [Part.partition_table_MAX]Part.Partition = undefined;
pub var default_zconfig: ZC.ZbootConfig = undefined;
const JsonUart = struct {
enable: u32,
tx: []u8,
};
const JsonSpiFlash = struct {
enable: u32,
cs: []u8,
sck: []u8,
mosi: []u8,
miso: []u8,
};
const JsonPartition = struct {
name: []u8,
flash_name: []u8,
offset: u32,
len: u32,
};
const JsonPartitionTable = struct {
patition: []JsonPartition,
};
const JsonConfig = struct {
uart: JsonUart,
spiflash: JsonSpiFlash,
partition_table: JsonPartitionTable,
};
const BUF_SIZE = 1024;
fn help() void {
std.debug.print("Usage: \n", .{});
std.debug.print(" zboot boot: gen stm32-zboot.bin [config.json] \n", .{});
std.debug.print(" zboot app: gen stm32-app.bin \n", .{});
std.debug.print(" zboot rbl <xxx.bin> <version>: tar xxx.bin to xxx.rbl \n", .{});
// std.debug.print(" zboot allbin: tar stm32-zboot|app|[swap] to all.bin \n", .{});
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
if (Debug) {
std.debug.print("arg: {s},{d}\n", .{ args[1], args.len });
}
var input_file: []u8 = undefined;
if (args.len > 1) {
// if argv[1] == boot gen stm32-zboot.bin
if (mem.eql(u8, args[1], "boot")) {
// if config.json not exist, gen config.json
std.fs.cwd().access("config.json", .{}) catch {
std.debug.print("=> gen config.json\n", .{});
try gen_configjson();
};
std.debug.print("=> gen stm32-zboot.bin\n", .{});
try gen_boot();
return;
} else if (mem.eql(u8, args[1], "rbl")) {
if (args.len < 3) {
help();
return;
}
input_file = args[2];
try gen_rbl(input_file, args[3]);
return;
} else if (mem.eql(u8, args[1], "app")) {
try gen_app();
std.debug.print("=> gen stm32-app.bin\n", .{});
return;
}
} else {
help();
return;
}
}
fn gen_configjson() !void {
const bin_file = try std.fs.cwd().createFile("./config.json", .{});
var buf_write = std.io.bufferedWriter(bin_file.writer());
var out_stream = buf_write.writer();
_ = out_stream.write(configjson) catch {
std.debug.print("write file error\n", .{});
return;
};
try buf_write.flush();
bin_file.close();
}
fn gen_boot() !void {
const bin_file = try std.fs.cwd().createFile("./stm32-zboot.bin", .{});
var buf_write = std.io.bufferedWriter(bin_file.writer());
var out_stream = buf_write.writer();
// find magic word 'Z' 'B' 'O' 'T' 0x544F425A from tail of bin file
var magic_offset: usize = 0;
var offset = stm32zboot.len - @sizeOf(ZC.ZbootConfig);
while (offset > 0) {
const magic_buf = stm32zboot[offset..(offset + 4)];
if (magic_buf[0] == 0x5A and magic_buf[1] == 0x42 and magic_buf[2] == 0x4F and magic_buf[3] == 0x54) {
magic_offset = offset;
break;
}
offset -= 1;
}
if (magic_offset == 0) {
std.debug.print("can't find ZBOOT_CONFIG_MAGIC\n", .{});
return;
}
_ = out_stream.write(stm32zboot[0..magic_offset]) catch {
std.debug.print("write file error\n", .{});
return;
};
// parse json file
try json_parse("config.json");
// write zbootconfig data
default_zconfig.magic = ZC.ZBOOT_CONFIG_MAGIC;
const slice_config = @as([*]u8, @ptrCast((&default_zconfig)))[0..(@sizeOf(ZC.ZbootConfig))];
_ = out_stream.write(slice_config) catch {
std.debug.print("write file error\n", .{});
return;
};
// write partition data
const slice = @as([*]u8, @ptrCast((&default_partition)))[0..(@sizeOf(Part.Partition) * partition_num)];
_ = out_stream.write(slice) catch {
std.debug.print("write file error\n", .{});
return;
};
try buf_write.flush();
bin_file.close();
}
fn gen_app() !void {
const bin_file = try std.fs.cwd().createFile("./stm32-app.bin", .{});
var buf_write = std.io.bufferedWriter(bin_file.writer());
var out_stream = buf_write.writer();
// find magic word 'Z' 'B' 'O' 'T' 0x544F425A from tail of bin file
var magic_offset: usize = 0;
var offset = stm32app.len - @sizeOf(ZC.ZbootConfig);
while (offset > 0) {
const magic_buf = stm32app[offset..(offset + 4)];
if (magic_buf[0] == 0x5A and magic_buf[1] == 0x42 and magic_buf[2] == 0x4F and magic_buf[3] == 0x54) {
magic_offset = offset;
break;
}
offset -= 1;
}
if (magic_offset == 0) {
std.debug.print("can't find ZBOOT_CONFIG_MAGIC\n", .{});
return;
}
_ = out_stream.write(stm32app[0..magic_offset]) catch {
std.debug.print("write file error\n", .{});
return;
};
// parse json file
try json_parse("config.json");
// write zbootconfig data
default_zconfig.magic = ZC.ZBOOT_CONFIG_MAGIC;
const slice_config = @as([*]u8, @ptrCast((&default_zconfig)))[0..(@sizeOf(ZC.ZbootConfig))];
_ = out_stream.write(slice_config) catch {
std.debug.print("write file error\n", .{});
return;
};
try buf_write.flush();
bin_file.close();
}
const FLZ_BUF_SIZE = 4096;
const TEMP_FILE = "_crom_tmp.tmp";
fn gen_rbl(input_file: []u8, version: []u8) !void {
var file = try std.fs.cwd().openFile(input_file, .{});
defer file.close();
// ready ota header
var fw_info: OTA.Ota_FW_Info = .{
.magic = .{ 'R', 'B', 'L', 0 },
.algo = .FASTLZ,
.algo2 = .NONE,
.time_stamp = 0,
.name = .{ 'a', 'p', 'p' } ++ .{0} ** 13,
.version = .{0} ** 24,
.sn = .{'0'} ** 24,
.body_crc = 0,
.hash_code = 0,
.raw_size = 0,
.pkg_size = 0,
.hdr_crc = 0,
};
const file_size = try file.getEndPos();
mem.copyForwards(u8, &fw_info.version, version[0..version.len]);
fw_info.raw_size = @intCast(file_size);
fw_info.time_stamp = @intCast(std.time.milliTimestamp());
// body crc
var buf_reader = std.io.bufferedReader(file.reader());
var in_stream = buf_reader.reader();
var buf: [FLZ_BUF_SIZE]u8 = undefined;
var crc: u32 = 0;
var hash: u32 = OTA.ZBOOT_HASH_FNV_SEED;
const crom_file = try std.fs.cwd().createFile(TEMP_FILE, .{});
while (true) {
const size = try in_stream.read(buf[0..]);
if (size == 0) {
break;
}
hash = OTA.calc_hash(hash, buf[0..size]);
var crom_buf: [FLZ_BUF_SIZE + OTA.fastlz_buffer_padding(FLZ_BUF_SIZE)]u8 = undefined;
for (&crom_buf) |*b| {
b.* = 0;
}
const compress_size = OTA.fastlz1_compress(crom_buf[0..], buf[0..size]);
const buffer_hdr: [4]u8 = .{
@intCast(compress_size / (1 << 24)),
@intCast((compress_size % (1 << 24)) / (1 << 16)),
@intCast((compress_size % (1 << 16)) / (1 << 8)),
@intCast(compress_size % (1 << 8)),
};
_ = crom_file.writeAll(buffer_hdr[0..]) catch {
std.debug.print("write file error\n", .{});
return;
};
_ = crom_file.writeAll(crom_buf[0..compress_size]) catch {
std.debug.print("write crom file error\n", .{});
return;
};
crc = OTA.crc32(crc, buffer_hdr[0..]);
crc = OTA.crc32(crc, crom_buf[0..compress_size]);
if (size < 1024) {
break;
}
}
crom_file.close();
// write rbl file
file = try std.fs.cwd().openFile(TEMP_FILE, .{});
buf_reader = std.io.bufferedReader(file.reader());
in_stream = buf_reader.reader();
var file_name_buf: [512]u8 = undefined;
const file_name = try std.fmt.bufPrint(&file_name_buf, "{s}.rbl", .{input_file});
std.debug.print("=> {s}\n", .{file_name});
const bin_file = try std.fs.cwd().createFile(file_name, .{});
var buf_write = std.io.bufferedWriter(bin_file.writer());
var out_stream = buf_write.writer();
var read_buf: [BUF_SIZE]u8 = undefined;
var offset: usize = 0;
var write_offset: usize = 0;
// write ota header
fw_info.body_crc = crc;
fw_info.hash_code = hash;
fw_info.pkg_size = @intCast(try file.getEndPos());
fw_info.hdr_crc = OTA.crc32(0, @as([*]u8, @ptrCast(&fw_info))[0..(@sizeOf(OTA.Ota_FW_Info) - @sizeOf(u32))]);
if (Debug) {
std.debug.print("crc: {x}, hash: {x}\n", .{ crc, hash });
std.debug.print("hdr_crc: {x}\n", .{fw_info.hdr_crc});
}
const slice_ota = @as([*]u8, @ptrCast((&fw_info)))[0..(@sizeOf(OTA.Ota_FW_Info))];
_ = out_stream.write(slice_ota) catch {
std.debug.print("write file error\n", .{});
return;
};
// read bin file and write to another file
while (true) {
const size = try in_stream.readAll(&read_buf);
offset += size;
if (Debug) {
std.debug.print("read: {d}, offset:{x}\n", .{ size, offset });
}
const ret = try out_stream.write(read_buf[0..size]);
write_offset += ret;
if (Debug) {
std.debug.print("write: {d}, offset:{x}\n", .{ ret, write_offset });
}
if (ret != size) {
std.debug.print("write file error: {}\n", .{ret});
return;
}
if (size < BUF_SIZE) {
try buf_write.flush();
break;
}
}
try buf_write.flush();
bin_file.close();
// delete crom file
try std.fs.cwd().deleteFile(TEMP_FILE);
}
pub fn json_parse(config_file: []const u8) !void {
const allocator = std.heap.page_allocator;
var buf: [4096]u8 = undefined;
var file = try std.fs.cwd().openFile(config_file, .{});
defer file.close();
const size = try file.readAll(buf[0..]);
if (Debug) {
// dump the file contents
std.debug.print("config.json: {s}\n", .{buf[0..size]});
}
const root = try std.json.parseFromSlice(JsonConfig, allocator, buf[0..size], .{ .allocate = .alloc_always });
const json_config = root.value;
// parse uart config
if (Debug) {
std.debug.print("uart enable:{d}\n", .{json_config.uart.enable});
std.debug.print("uart tx:{s}\n", .{json_config.uart.tx});
}
if (json_config.uart.tx.len > ZC.PIN_NAME_MAX) {
std.debug.print("uart tx is too long\n", .{});
return;
}
if (json_config.uart.enable == 1) {
default_zconfig.uart.enable = true;
} else {
default_zconfig.uart.enable = false;
}
mem.copyForwards(u8, &default_zconfig.uart.tx, json_config.uart.tx[0..json_config.uart.tx.len]);
// parse spiflash config
if (Debug) {
std.debug.print("spi enable:{d}\n", .{json_config.spiflash.enable});
std.debug.print("spi cs:{s}\n", .{json_config.spiflash.cs});
std.debug.print("spi sck:{s}\n", .{json_config.spiflash.sck});
std.debug.print("spi mosi:{s}\n", .{json_config.spiflash.mosi});
std.debug.print("spi miso:{s}\n", .{json_config.spiflash.miso});
}
if (json_config.spiflash.enable == 1) {
default_zconfig.spiflash.enable = true;
} else {
default_zconfig.spiflash.enable = false;
}
mem.copyForwards(u8, &default_zconfig.spiflash.cs, json_config.spiflash.cs[0..json_config.spiflash.cs.len]);
mem.copyForwards(u8, &default_zconfig.spiflash.sck, json_config.spiflash.sck[0..json_config.spiflash.sck.len]);
mem.copyForwards(u8, &default_zconfig.spiflash.mosi, json_config.spiflash.mosi[0..json_config.spiflash.mosi.len]);
mem.copyForwards(u8, &default_zconfig.spiflash.miso, json_config.spiflash.miso[0..json_config.spiflash.miso.len]);
// parse fal partition config
for (json_config.partition_table.patition, 0..) |patition, i| {
if (Debug) {
std.debug.print("config.patition[{d}].name: {s}\n", .{ i, patition.name });
std.debug.print("config.patition[{d}].flash_name: {s}\n", .{ i, patition.flash_name });
std.debug.print("config.patition[{d}].offset: {d}\n", .{ i, patition.offset });
std.debug.print("config.patition[{d}].len: {d}\n", .{ i, patition.len });
}
if (patition.flash_name.len > Part.FAL_DEV_NAME_MAX) {
std.debug.print("flash_name is too long\n", .{});
return;
}
if (patition.name.len > Part.FAL_DEV_NAME_MAX) {
std.debug.print("name is too long\n", .{});
return;
}
default_partition[i].magic_word = Part.FAL_MAGIC_WORD;
mem.copyForwards(u8, &default_partition[i].name, patition.name[0..patition.name.len]);
mem.copyForwards(u8, &default_partition[i].flash_name, patition.flash_name[0..patition.flash_name.len]);
default_partition[i].offset = patition.offset * KiB;
default_partition[i].len = patition.len * KiB;
default_partition[i].reserved = 0;
partition_num = @intCast(i + 1);
}
}