1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 Marvell International Ltd.
17 #include <arpa/inet.h>
18 #include <linux/compiler.h>
19 #include <u-boot/crc.h>
23 #include "../arch/mips/mach-octeon/include/mach/cvmx-bootloader.h"
25 #define BUF_SIZE (16 * 1024)
29 #define WOFFSETOF(type, elem) (offsetof(type, elem) / 4)
31 static int stage2_flag;
32 static int stage_1_5_flag;
33 static int stage_1_flag;
35 /* Getoptions variables must be global */
36 static int failsafe_flag;
37 static int pciboot_flag;
40 static const struct option long_options[] = {
41 /* These options set a flag. */
42 {"failsafe", no_argument, &failsafe_flag, 1},
43 {"pciboot", no_argument, &pciboot_flag, 1},
44 {"nandstage2", no_argument, &stage2_flag, 1},
45 {"spistage2", no_argument, &stage2_flag, 1},
46 {"norstage2", no_argument, &stage2_flag, 1},
47 {"stage2", no_argument, &stage2_flag, 1},
48 {"stage1.5", no_argument, &stage_1_5_flag, 1},
49 {"stage1", no_argument, &stage_1_flag, 1},
50 {"environment", no_argument, &env_flag, 1},
52 * These options don't set a flag.
53 * We distinguish them by their indices.
55 {"board", required_argument, 0, 0},
56 {"text_base", required_argument, 0, 0},
60 static int lookup_board_type(char *board_name)
66 /* Detect stage 2 bootloader boards */
67 if (strcasestr(board_name, "_stage2")) {
68 printf("Stage 2 bootloader detected from substring %s in name %s\n",
69 "_stage2", board_name);
72 printf("Stage 2 bootloader NOT detected from name \"%s\"\n",
76 if (strcasestr(board_name, "_stage1")) {
77 printf("Stage 1 bootloader detected from substring %s in name %s\n",
78 "_stage1", board_name);
82 /* Generic is a special case since there are numerous sub-types */
83 if (!strncasecmp("generic", board_name, strlen("generic")))
84 return CVMX_BOARD_TYPE_GENERIC;
87 * If we're an eMMC stage 2 bootloader, cut off the _emmc_stage2
90 substr = strcasestr(board_name, "_emmc_stage2");
91 if (substr && (substr[strlen("_emmc_stage2")] == '\0')) {
92 /*return CVMX_BOARD_TYPE_GENERIC;*/
94 printf(" Converting board name %s to ", board_name);
96 printf("%s\n", board_name);
100 * If we're a NAND stage 2 bootloader, cut off the _nand_stage2
103 substr = strcasestr(board_name, "_nand_stage2");
104 if (substr && (substr[strlen("_nand_stage2")] == '\0')) {
105 /*return CVMX_BOARD_TYPE_GENERIC;*/
107 printf(" Converting board name %s to ", board_name);
109 printf("%s\n", board_name);
113 * If we're a SPI stage 2 bootloader, cut off the _spi_stage2
116 substr = strcasestr(board_name, "_spi_stage2");
117 if (substr && (substr[strlen("_spi_stage2")] == '\0')) {
118 printf(" Converting board name %s to ", board_name);
120 printf("%s\n", board_name);
123 for (i = CVMX_BOARD_TYPE_NULL; i < CVMX_BOARD_TYPE_MAX; i++)
124 if (!strcasecmp(cvmx_board_type_to_string(i), board_name))
127 for (i = CVMX_BOARD_TYPE_CUST_DEFINED_MIN;
128 i < CVMX_BOARD_TYPE_CUST_DEFINED_MAX; i++)
129 if (!strncasecmp(cvmx_board_type_to_string(i), board_name,
130 strlen(cvmx_board_type_to_string(i))))
133 for (i = CVMX_BOARD_TYPE_CUST_PRIVATE_MIN;
134 i < CVMX_BOARD_TYPE_CUST_PRIVATE_MAX; i++)
135 if (!strncasecmp(cvmx_board_type_to_string(i), board_name,
136 strlen(cvmx_board_type_to_string(i))))
142 static void usage(void)
144 printf("Usage: update_octeon_header <filename> <board_name> [--failsafe] [--text_base=0xXXXXX]\n");
147 int main(int argc, char *argv[])
150 uint8_t buf[BUF_SIZE];
151 uint32_t data_crc = 0;
154 struct bootloader_header header;
155 char filename[NAME_LEN];
157 int option_index = 0; /* getopt_long stores the option index here. */
158 char board_name[NAME_LEN] = { 0 };
159 char tmp_board_name[NAME_LEN] = { 0 };
162 unsigned long long address = 0;
164 const char *type_str = NULL;
165 int hdr_size = sizeof(struct bootloader_header);
168 * Compile time check, if the size of the bootloader_header structure
171 compiletime_assert(sizeof(struct bootloader_header) == 192,
172 "Octeon bootloader header size changed (!= 192)!");
174 /* Bail out, if argument count is incorrect */
180 debug("header size is: %d bytes\n", hdr_size);
182 /* Parse command line options using getopt_long */
184 c = getopt_long(argc, argv, "h", long_options, &option_index);
186 /* Detect the end of the options. */
191 /* All long options handled in case 0 */
193 /* If this option set a flag, do nothing else now. */
194 if (long_options[option_index].flag != 0)
196 debug("option(l) %s", long_options[option_index].name);
202 debug(" with arg %s\n", optarg);
204 if (!strcmp(long_options[option_index].name, "board")) {
205 if (strlen(optarg) >= NAME_LEN) {
206 printf("strncpy() issue detected!");
209 strncpy(board_name, optarg, NAME_LEN);
211 printf("Using user supplied board name: %s\n",
213 } else if (!strcmp(long_options[option_index].name,
215 address = strtoull(optarg, NULL, 0);
216 printf("Address of image is: 0x%llx\n",
217 (unsigned long long)address);
218 if (!(address & 0xFFFFFFFFULL << 32)) {
219 if (address & 1 << 31) {
220 address |= 0xFFFFFFFFULL << 32;
221 printf("Converting address to 64 bit compatibility space: 0x%llx\n",
230 /* getopt_long already printed an error message. */
241 * We only support one argument - an optional bootloader
244 if (argc - optind > 2) {
245 fprintf(stderr, "non-option ARGV-elements: ");
246 while (optind < argc)
247 fprintf(stderr, "%s ", argv[optind++]);
248 fprintf(stderr, "\n");
255 if (strlen(argv[optind]) >= NAME_LEN) {
256 fprintf(stderr, "strncpy() issue detected!");
259 strncpy(filename, argv[optind], NAME_LEN);
261 if (board_name[0] == '\0') {
262 if (strlen(argv[optind + 1]) >= NAME_LEN) {
263 fprintf(stderr, "strncpy() issue detected!");
266 strncpy(board_name, argv[optind + 1], NAME_LEN);
269 if (strlen(board_name) >= NAME_LEN) {
270 fprintf(stderr, "strncpy() issue detected!");
273 strncpy(tmp_board_name, board_name, NAME_LEN);
275 fd = open(filename, O_RDWR);
277 fprintf(stderr, "Unable to open file: %s\n", filename);
282 printf("Setting failsafe flag\n");
284 if (strlen(board_name)) {
287 printf("Supplied board name of: %s\n", board_name);
289 if (strstr(board_name, "failsafe")) {
291 printf("Setting failsafe flag based on board name\n");
293 /* Skip leading octeon_ if present. */
294 if (!strncmp(board_name, "octeon_", 7))
298 * Check to see if 'failsafe' is in the name. If so, set the
299 * failsafe flag. Also, ignore extra trailing characters on
300 * passed parameter when comparing against board names.
301 * We actually use the configuration name from u-boot, so it
302 * may have some other variant names. Variants other than
303 * failsafe _must_ be passed to this program explicitly
306 board_type = lookup_board_type(board_name + offset);
308 /* Retry with 'cust_' prefix to catch boards that are
309 * in the customer section (such as nb5)
311 sprintf(tmp_board_name, "cust_%s", board_name + offset);
312 board_type = lookup_board_type(tmp_board_name);
315 /* reset to original value */
316 strncpy(tmp_board_name, board_name, NAME_LEN);
319 * Retry with 'cust_private_' prefix to catch boards
320 * that are in the customer private section
322 sprintf(tmp_board_name, "cust_private_%s",
323 board_name + offset);
324 board_type = lookup_board_type(tmp_board_name);
329 "ERROR: unable to determine board type\n");
332 printf("Board type is: %d: %s\n", board_type,
333 cvmx_board_type_to_string(board_type));
335 fprintf(stderr, "Board name must be specified!\n");
340 * Check to see if there is either an existing header, or that there
341 * are zero valued bytes where we want to put the header
343 len = read(fd, buf, BUF_SIZE);
346 * Copy the header, as the first word (jump instruction, needs
347 * to remain the same.
349 memcpy(&header, buf, hdr_size);
351 * Check to see if we have zero bytes (excluding first 4, which
352 * are the jump instruction)
354 for (i = 1; i < hdr_size / 4; i++) {
355 if (((uint32_t *)buf)[i]) {
357 "ERROR: non-zero word found %x in location %d required for header, aborting\n",
358 ((uint32_t *)buf)[i], i);
362 printf("Zero bytes found in header location, adding header.\n");
365 fprintf(stderr, "Unable to read from file %s\n", filename);
369 /* Read data bytes and generate CRC */
370 lseek(fd, hdr_size, SEEK_SET);
372 while ((len = read(fd, buf, BUF_SIZE)) > 0) {
373 data_crc = crc32(data_crc, buf, len);
376 printf("CRC of data: 0x%x, length: %d\n", data_crc, data_len);
378 /* Now create the new header */
379 header.magic = htonl(BOOTLOADER_HEADER_MAGIC);
380 header.maj_rev = htons(BOOTLOADER_HEADER_CURRENT_MAJOR_REV);
381 header.min_rev = htons(BOOTLOADER_HEADER_CURRENT_MINOR_REV);
382 header.dlen = htonl(data_len);
383 header.dcrc = htonl(data_crc);
384 header.board_type = htons(board_type);
385 header.address = address;
387 header.flags |= htonl(BL_HEADER_FLAG_FAILSAFE);
389 printf("Stage 2 flag is %sset\n", stage2_flag ? "" : "not ");
390 printf("Stage 1 flag is %sset\n", stage_1_flag ? "" : "not ");
392 header.image_type = htons(BL_HEADER_IMAGE_PCIBOOT);
393 else if (stage2_flag)
394 header.image_type = htons(BL_HEADER_IMAGE_STAGE2);
395 else if (stage_1_flag)
396 header.image_type = htons(BL_HEADER_IMAGE_STAGE1);
398 header.image_type = htons(BL_HEADER_IMAGE_UBOOT_ENV);
399 else if (stage_1_5_flag || stage_1_flag)
400 header.image_type = htons(BL_HEADER_IMAGE_PRE_UBOOT);
402 header.image_type = htons(BL_HEADER_IMAGE_NOR);
404 switch (ntohs(header.image_type)) {
405 case BL_HEADER_IMAGE_UNKNOWN:
406 type_str = "Unknown";
408 case BL_HEADER_IMAGE_STAGE1:
409 type_str = "Stage 1";
411 case BL_HEADER_IMAGE_STAGE2:
412 type_str = "Stage 2";
414 case BL_HEADER_IMAGE_PRE_UBOOT:
415 type_str = "Pre-U-Boot";
417 case BL_HEADER_IMAGE_STAGE3:
418 type_str = "Stage 3";
420 case BL_HEADER_IMAGE_NOR:
423 case BL_HEADER_IMAGE_PCIBOOT:
424 type_str = "PCI Boot";
426 case BL_HEADER_IMAGE_UBOOT_ENV:
427 type_str = "U-Boot Environment";
430 if (ntohs(header.image_type) >= BL_HEADER_IMAGE_CUST_RESERVED_MIN &&
431 ntohs(header.image_type) <= BL_HEADER_IMAGE_CUST_RESERVED_MAX)
432 type_str = "Customer Reserved";
434 type_str = "Unsupported";
436 printf("Header image type: %s\n", type_str);
437 header.hlen = htons(hdr_size);
439 /* Now compute header CRC over all of the header excluding the CRC */
440 header.hcrc = crc32(0, (void *)&header, 12);
441 header.hcrc = htonl(crc32(header.hcrc, ((void *)&(header)) + 16,
444 /* Seek to beginning of file */
445 lseek(fd, 0, SEEK_SET);
447 /* Write header to file */
448 ret = write(fd, &header, hdr_size);
454 printf("Header CRC: 0x%x\n", ntohl(header.hcrc));