|
| 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 | + fprintf(stderr, "Number of slots: %d\n", n_keys); |
| 67 | + fprintf(stderr, "%s size: %d\n", outfile, slot_size * n_keys + sizeof(struct wolfBoot_otp_hdr)); |
| 68 | + |
| 69 | + ofd = open(outfile, O_WRONLY|O_CREAT|O_TRUNC, 0600); |
| 70 | + if (ofd < 0) { |
| 71 | + perror("opening output file"); |
| 72 | + exit(2); |
| 73 | + } |
| 74 | + |
| 75 | + /* Write the header to the beginning of the OTP binary file */ |
| 76 | + if (write(ofd, &hdr, sizeof(hdr)) != sizeof(hdr)) { |
| 77 | + fprintf(stderr, "Error writing to %s: %s\n", outfile, strerror(errno)); |
| 78 | + } |
| 79 | + |
| 80 | + for (i = 0; i < n_keys; i++) { |
| 81 | + /* Write each public key to its slot in OTP */ |
| 82 | + if (write(ofd, &PubKeys[i], |
| 83 | + slot_size) < 0) { |
| 84 | + fprintf(stderr, "Error adding key %d to %s: %s\n", i, outfile, strerror(errno)); |
| 85 | + exit(3); |
| 86 | + } |
| 87 | + } |
| 88 | + fprintf(stderr, "%s successfully created.\nGoodbye.\n", outfile); |
| 89 | + close(ofd); |
| 90 | + return 0; |
| 91 | +} |
0 commit comments