Skip to content

Commit 2133155

Browse files
committed
Add command line tool to generate otp.bin
1 parent 626fcf0 commit 2133155

5 files changed

Lines changed: 101 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ tools/keytools/otp/otp-keystore-primer
7575
tools/delta/bmdiff
7676
tools/delta/bmpatch
7777

78+
# otp-keystore-gen binary
79+
tools/keytools/otp/otp-keystore-gen
80+
7881
# Vim swap files
7982
.*.swp
8083

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ cppcheck:
377377

378378
otp: tools/keytools/otp/otp-keystore-primer.bin FORCE
379379

380+
otpgen:
381+
make -C tools/keytools/otp otp-keystore-gen
382+
380383
tools/keytools/otp/otp-keystore-primer.bin: FORCE
381384
make -C tools/keytools/otp clean
382385
make -C tools/keytools/otp

include/otp_keystore.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
#include "hal/stm32h7.h"
3535
#elif defined TARGET_stm32h5
3636
#include "hal/stm32h5.h"
37-
#else
38-
#error "Unsupported target for OTP"
3937
#endif
4038

4139
#include "keystore.h"

tools/keytools/otp/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ CC=$(CROSS_COMPILE)gcc
3232
OBJCOPY?=$(CROSS_COMPILE)objcopy
3333
SIZE?=$(CROSS_COMPILE)size
3434

35+
all: otp-keystore-primer.bin otp-keystore-gen
36+
37+
otp-keystore-gen: otp-keystore-gen.c ../../../src/keystore.c
38+
gcc -o $@ $^ -I. -I../../../ -I../../../include -DFLASH_OTP_KEYSTORE
39+
40+
3541
otp-keystore-primer.bin: otp-keystore-primer.elf
3642
$(Q)$(OBJCOPY) -O binary $(^) $(@)
3743

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* otp-keystore-primer.c
2+
*
3+
* Command line utility to create a OTP image
4+
*
5+
*
6+
* Copyright (C) 2024 wolfSSL Inc.
7+
*
8+
* This file is part of wolfBoot.
9+
*
10+
* wolfBoot is free software; you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation; either version 3 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* wolfBoot is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program; if not, write to the Free Software
22+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
23+
*/
24+
#include <stdint.h>
25+
#include <stdio.h>
26+
#include <string.h>
27+
#include <stddef.h>
28+
#include <stdlib.h>
29+
#include <fcntl.h>
30+
#include <unistd.h>
31+
#include <errno.h>
32+
33+
#define OTP_SIZE 4096
34+
35+
#include "wolfboot/wolfboot.h"
36+
#include "keystore.h"
37+
#include "otp_keystore.h"
38+
39+
extern struct keystore_slot PubKeys[];
40+
41+
const char outfile[] = "otp.bin";
42+
43+
int main(void)
44+
{
45+
int n_keys = keystore_num_pubkeys();
46+
int i;
47+
struct wolfBoot_otp_hdr hdr;
48+
uint32_t tot_len;
49+
int ofd;
50+
int slot_size;
51+
52+
memcpy(hdr.keystore_hdr_magic, KEYSTORE_HDR_MAGIC, 8);
53+
hdr.item_count = n_keys;
54+
hdr.flags = 0;
55+
hdr.version = WOLFBOOT_VERSION;
56+
57+
/* Sanity check to avoid writing an empty keystore */
58+
if (n_keys < 1) {
59+
fprintf(stderr, "Error: too few keys (%d), refusing to create %s\n", n_keys, outfile);
60+
exit(1);
61+
}
62+
63+
slot_size = keystore_get_size(0);
64+
slot_size += KEYSTORE_HDR_SIZE;
65+
fprintf(stderr, "Slot size: %d\n", slot_size);
66+
67+
ofd = open(outfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
68+
if (ofd < 0) {
69+
perror("opening output file");
70+
exit(2);
71+
}
72+
73+
/* Write the header to the beginning of the OTP binary file */
74+
if (write(ofd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
75+
fprintf(stderr, "Error writing to %s: %s\n", outfile, strerror(errno));
76+
}
77+
78+
for (i = 0; i < n_keys; i++) {
79+
/* Write each public key to its slot in OTP */
80+
if (write(ofd, &PubKeys[i],
81+
slot_size) < 0) {
82+
fprintf(stderr, "Error adding key %d to %s: %s\n", i, outfile, strerror(errno));
83+
exit(3);
84+
}
85+
}
86+
fprintf(stderr, "%s successfully created.\nGoodbye.\n", outfile);
87+
close(ofd);
88+
return 0;
89+
}

0 commit comments

Comments
 (0)