1 // SPDX-License-Identifier: GPL-2.0+
4 * Support for the "SEAttle iMAge" SEAMA NAND image format
11 * All SEAMA data is stored in the flash in "network endianness"
12 * i.e. big endian, which means that it needs to be byte-swapped
13 * on all little endian platforms.
15 * structure for a SEAMA entity in NAND flash:
17 * 32 bit SEAMA magic 0x5EA3A417
19 * 16 bit metadata size (following the header)
21 * 16 bytes MD5 digest of the image
25 * Then if a new SEAMA magic follows, that is the next image.
28 #define SEAMA_MAGIC 0x5EA3A417
29 #define SEAMA_HDR_NO_META_SZ 28
30 #define SEAMA_MAX_META_SZ (1024 - SEAMA_HDR_NO_META_SZ)
37 u8 metadata[SEAMA_MAX_META_SZ];
40 static struct seama_header shdr;
42 static int env_set_val(const char *varname, ulong val)
46 ret = env_set_hex(varname, val);
48 printf("Failed to %s env var\n", varname);
53 static int do_seama_load_image(struct cmd_tbl *cmdtp, int flag, int argc,
58 unsigned long image_index;
67 if (argc < 2 || argc > 3)
70 load_addr = hextoul(argv[1], NULL);
72 printf("Invalid load address\n");
76 /* Can be 0 for first image */
77 image_index = hextoul(argv[2], NULL);
79 /* We only support one NAND, the first one */
81 mtd = get_nand_dev_by_index(0);
83 printf("NAND Device 0 not available\n");
84 return CMD_RET_FAILURE;
87 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
88 board_nand_select_device(mtd_to_nand(mtd), 0);
91 printf("Loading SEAMA image %lu from %s\n", image_index, mtd->name);
93 readsz = sizeof(shdr);
95 ret = nand_read_skip_bad(mtd, 0, &readsz, NULL, mtd->size,
98 printf("Read error reading SEAMA header\n");
99 return CMD_RET_FAILURE;
102 if (shdr.magic != SEAMA_MAGIC) {
103 printf("Invalid SEAMA image magic: 0x%08x\n", shdr.magic);
104 return CMD_RET_FAILURE;
107 /* Only the lower 16 bits are valid */
108 shdr.meta_size &= 0xFFFF;
110 if (env_set_val("seama_image_size", 0))
111 return CMD_RET_FAILURE;
113 printf("SEMA IMAGE:\n");
114 printf(" metadata size %d\n", shdr.meta_size);
115 printf(" image size %d\n", shdr.image_size);
116 printf(" checksum %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
117 shdr.md5[0], shdr.md5[1], shdr.md5[2], shdr.md5[3],
118 shdr.md5[4], shdr.md5[5], shdr.md5[6], shdr.md5[7],
119 shdr.md5[8], shdr.md5[9], shdr.md5[10], shdr.md5[11],
120 shdr.md5[12], shdr.md5[13], shdr.md5[14], shdr.md5[15]);
122 /* TODO: handle metadata if needed */
124 len = shdr.image_size;
125 if (env_set_val("seama_image_size", len))
126 return CMD_RET_FAILURE;
128 /* We need to include the header (read full pages) */
129 readsz = shdr.image_size + SEAMA_HDR_NO_META_SZ + shdr.meta_size;
130 ret = nand_read_skip_bad(mtd, 0, &readsz, NULL, mtd->size,
131 (u_char *)load_addr);
133 printf("Read error reading SEAMA main image\n");
134 return CMD_RET_FAILURE;
137 /* We use a temporary variable tmp to avoid to hairy casts */
138 start = (u32 *)load_addr;
140 tmp += SEAMA_HDR_NO_META_SZ + shdr.meta_size;
142 tmp += shdr.image_size;
145 printf("Decoding SEAMA image 0x%08x..0x%08x to 0x%08x\n",
146 (u32)offset, (u32)end, (u32)start);
147 for (; start < end; start++, offset++)
148 *start = be32_to_cpu(*offset);
150 return CMD_RET_SUCCESS;
154 (seama, 3, 1, do_seama_load_image,
155 "Load the SEAMA image and sets envs",
156 "seama <addr> <imageindex>\n"