2 * Freescale i.MX28 image generator
4 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5 * on behalf of DENX Software Engineering GmbH
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <sys/types.h>
20 * TWEAK this if you have blown any OCOTP fuses.
22 #define STRIDE_PAGES 64
23 #define STRIDE_COUNT 4
26 * Layout for 256Mb big NAND with 2048b page size, 64b OOB size and
29 * TWEAK this if you have different kind of NAND chip.
31 uint32_t nand_writesize = 2048;
32 uint32_t nand_oobsize = 64;
33 uint32_t nand_erasesize = 128 * 1024;
36 * Sector on which the SigmaTel boot partition (0x53) starts.
38 uint32_t sd_sector = 2048;
41 * Each of the U-Boot bootstreams is at maximum 1MB big.
43 * TWEAK this if, for some wild reason, you need to boot bigger image.
45 #define MAX_BOOTSTREAM_SIZE (1 * 1024 * 1024)
47 /* i.MX28 NAND controller-specific constants. DO NOT TWEAK! */
48 #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
49 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE 512
50 #define MXS_NAND_METADATA_SIZE 10
51 #define MXS_NAND_COMMAND_BUFFER_SIZE 32
53 struct mx28_nand_fcb {
60 uint8_t address_setup;
62 uint8_t nand_timing_state;
67 uint32_t page_data_size;
68 uint32_t total_page_size;
69 uint32_t sectors_per_block;
70 uint32_t number_of_nands; /* Ignored */
71 uint32_t total_internal_die; /* Ignored */
72 uint32_t cell_type; /* Ignored */
73 uint32_t ecc_block_n_ecc_type;
74 uint32_t ecc_block_0_size;
75 uint32_t ecc_block_n_size;
76 uint32_t ecc_block_0_ecc_type;
77 uint32_t metadata_bytes;
78 uint32_t num_ecc_blocks_per_page;
79 uint32_t ecc_block_n_ecc_level_sdk; /* Ignored */
80 uint32_t ecc_block_0_size_sdk; /* Ignored */
81 uint32_t ecc_block_n_size_sdk; /* Ignored */
82 uint32_t ecc_block_0_ecc_level_sdk; /* Ignored */
83 uint32_t num_ecc_blocks_per_page_sdk; /* Ignored */
84 uint32_t metadata_bytes_sdk; /* Ignored */
85 uint32_t erase_threshold;
87 uint32_t patch_sectors;
88 uint32_t firmware1_starting_sector;
89 uint32_t firmware2_starting_sector;
90 uint32_t sectors_in_firmware1;
91 uint32_t sectors_in_firmware2;
92 uint32_t dbbt_search_area_start_address;
93 uint32_t badblock_marker_byte;
94 uint32_t badblock_marker_start_bit;
95 uint32_t bb_marker_physical_offset;
98 struct mx28_nand_dbbt {
100 uint32_t fingerprint;
103 uint32_t number_2k_pages_bb;
106 struct mx28_nand_bbt {
109 uint32_t badblock[510];
112 struct mx28_sd_drive_info {
116 uint32_t first_sector_number;
117 uint32_t sector_count;
120 struct mx28_sd_config_block {
122 uint32_t primary_boot_tag;
123 uint32_t secondary_boot_tag;
125 struct mx28_sd_drive_info drv_info[1];
128 static inline uint32_t mx28_nand_ecc_size_in_bits(uint32_t ecc_strength)
130 return ecc_strength * 13;
133 static inline uint32_t mx28_nand_get_ecc_strength(uint32_t page_data_size,
134 uint32_t page_oob_size)
136 if (page_data_size == 2048)
139 if (page_data_size == 4096) {
140 if (page_oob_size == 128)
143 if (page_oob_size == 218)
150 static inline uint32_t mx28_nand_get_mark_offset(uint32_t page_data_size,
151 uint32_t ecc_strength)
153 uint32_t chunk_data_size_in_bits;
154 uint32_t chunk_ecc_size_in_bits;
155 uint32_t chunk_total_size_in_bits;
156 uint32_t block_mark_chunk_number;
157 uint32_t block_mark_chunk_bit_offset;
158 uint32_t block_mark_bit_offset;
160 chunk_data_size_in_bits = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 8;
161 chunk_ecc_size_in_bits = mx28_nand_ecc_size_in_bits(ecc_strength);
163 chunk_total_size_in_bits =
164 chunk_data_size_in_bits + chunk_ecc_size_in_bits;
166 /* Compute the bit offset of the block mark within the physical page. */
167 block_mark_bit_offset = page_data_size * 8;
169 /* Subtract the metadata bits. */
170 block_mark_bit_offset -= MXS_NAND_METADATA_SIZE * 8;
173 * Compute the chunk number (starting at zero) in which the block mark
176 block_mark_chunk_number =
177 block_mark_bit_offset / chunk_total_size_in_bits;
180 * Compute the bit offset of the block mark within its chunk, and
183 block_mark_chunk_bit_offset = block_mark_bit_offset -
184 (block_mark_chunk_number * chunk_total_size_in_bits);
186 if (block_mark_chunk_bit_offset > chunk_data_size_in_bits)
190 * Now that we know the chunk number in which the block mark appears,
191 * we can subtract all the ECC bits that appear before it.
193 block_mark_bit_offset -=
194 block_mark_chunk_number * chunk_ecc_size_in_bits;
196 return block_mark_bit_offset;
199 static inline uint32_t mx28_nand_mark_byte_offset(void)
201 uint32_t ecc_strength;
202 ecc_strength = mx28_nand_get_ecc_strength(nand_writesize, nand_oobsize);
203 return mx28_nand_get_mark_offset(nand_writesize, ecc_strength) >> 3;
206 static inline uint32_t mx28_nand_mark_bit_offset(void)
208 uint32_t ecc_strength;
209 ecc_strength = mx28_nand_get_ecc_strength(nand_writesize, nand_oobsize);
210 return mx28_nand_get_mark_offset(nand_writesize, ecc_strength) & 0x7;
213 static uint32_t mx28_nand_block_csum(uint8_t *block, uint32_t size)
218 for (i = 0; i < size; i++)
221 return csum ^ 0xffffffff;
224 static struct mx28_nand_fcb *mx28_nand_get_fcb(uint32_t size)
226 struct mx28_nand_fcb *fcb;
227 uint32_t bcb_size_bytes;
228 uint32_t stride_size_bytes;
229 uint32_t bootstream_size_pages;
230 uint32_t fw1_start_page;
231 uint32_t fw2_start_page;
233 fcb = malloc(nand_writesize);
235 printf("MX28 NAND: Unable to allocate FCB\n");
239 memset(fcb, 0, nand_writesize);
241 fcb->fingerprint = 0x20424346;
242 fcb->version = 0x01000000;
245 * FIXME: These here are default values as found in kobs-ng. We should
246 * probably retrieve the data from NAND or something.
248 fcb->timing.data_setup = 80;
249 fcb->timing.data_hold = 60;
250 fcb->timing.address_setup = 25;
251 fcb->timing.dsample_time = 6;
253 fcb->page_data_size = nand_writesize;
254 fcb->total_page_size = nand_writesize + nand_oobsize;
255 fcb->sectors_per_block = nand_erasesize / nand_writesize;
257 fcb->num_ecc_blocks_per_page = (nand_writesize / 512) - 1;
258 fcb->ecc_block_0_size = 512;
259 fcb->ecc_block_n_size = 512;
260 fcb->metadata_bytes = 10;
262 if (nand_writesize == 2048) {
263 fcb->ecc_block_n_ecc_type = 4;
264 fcb->ecc_block_0_ecc_type = 4;
265 } else if (nand_writesize == 4096) {
266 if (nand_oobsize == 128) {
267 fcb->ecc_block_n_ecc_type = 4;
268 fcb->ecc_block_0_ecc_type = 4;
269 } else if (nand_oobsize == 218) {
270 fcb->ecc_block_n_ecc_type = 8;
271 fcb->ecc_block_0_ecc_type = 8;
275 if (fcb->ecc_block_n_ecc_type == 0) {
276 printf("MX28 NAND: Unsupported NAND geometry\n");
281 fcb->patch_sectors = 0;
283 fcb->badblock_marker_byte = mx28_nand_mark_byte_offset();
284 fcb->badblock_marker_start_bit = mx28_nand_mark_bit_offset();
285 fcb->bb_marker_physical_offset = nand_writesize;
287 stride_size_bytes = STRIDE_PAGES * nand_writesize;
288 bcb_size_bytes = stride_size_bytes * STRIDE_COUNT;
290 bootstream_size_pages = (size + (nand_writesize - 1)) /
293 fw1_start_page = 2 * bcb_size_bytes / nand_writesize;
294 fw2_start_page = (2 * bcb_size_bytes + MAX_BOOTSTREAM_SIZE) /
297 fcb->firmware1_starting_sector = fw1_start_page;
298 fcb->firmware2_starting_sector = fw2_start_page;
299 fcb->sectors_in_firmware1 = bootstream_size_pages;
300 fcb->sectors_in_firmware2 = bootstream_size_pages;
302 fcb->dbbt_search_area_start_address = STRIDE_PAGES * STRIDE_COUNT;
311 static struct mx28_nand_dbbt *mx28_nand_get_dbbt(void)
313 struct mx28_nand_dbbt *dbbt;
315 dbbt = malloc(nand_writesize);
317 printf("MX28 NAND: Unable to allocate DBBT\n");
321 memset(dbbt, 0, nand_writesize);
323 dbbt->fingerprint = 0x54424244;
329 static inline uint8_t mx28_nand_parity_13_8(const uint8_t b)
331 uint32_t parity = 0, tmp;
333 tmp = ((b >> 6) ^ (b >> 5) ^ (b >> 3) ^ (b >> 2)) & 1;
336 tmp = ((b >> 7) ^ (b >> 5) ^ (b >> 4) ^ (b >> 2) ^ (b >> 1)) & 1;
339 tmp = ((b >> 7) ^ (b >> 6) ^ (b >> 5) ^ (b >> 1) ^ (b >> 0)) & 1;
342 tmp = ((b >> 7) ^ (b >> 4) ^ (b >> 3) ^ (b >> 0)) & 1;
345 tmp = ((b >> 6) ^ (b >> 4) ^ (b >> 3) ^
346 (b >> 2) ^ (b >> 1) ^ (b >> 0)) & 1;
352 static uint8_t *mx28_nand_fcb_block(struct mx28_nand_fcb *fcb)
358 block = malloc(nand_writesize + nand_oobsize);
360 printf("MX28 NAND: Unable to allocate FCB block\n");
364 memset(block, 0, nand_writesize + nand_oobsize);
366 /* Update the FCB checksum */
367 fcb->checksum = mx28_nand_block_csum(((uint8_t *)fcb) + 4, 508);
369 /* Figure 12-11. in iMX28RM, rev. 1, says FCB is at offset 12 */
370 memcpy(block + 12, fcb, sizeof(struct mx28_nand_fcb));
372 /* ECC is at offset 12 + 512 */
373 ecc = block + 12 + 512;
375 /* Compute the ECC parity */
376 for (i = 0; i < sizeof(struct mx28_nand_fcb); i++)
377 ecc[i] = mx28_nand_parity_13_8(block[i + 12]);
382 static int mx28_nand_write_fcb(struct mx28_nand_fcb *fcb, char *buf)
389 fcbblock = mx28_nand_fcb_block(fcb);
393 for (i = 0; i < STRIDE_PAGES * STRIDE_COUNT; i += STRIDE_PAGES) {
394 offset = i * nand_writesize;
395 memcpy(buf + offset, fcbblock, nand_writesize + nand_oobsize);
402 static int mx28_nand_write_dbbt(struct mx28_nand_dbbt *dbbt, char *buf)
405 int i = STRIDE_PAGES * STRIDE_COUNT;
407 for (; i < 2 * STRIDE_PAGES * STRIDE_COUNT; i += STRIDE_PAGES) {
408 offset = i * nand_writesize;
409 memcpy(buf + offset, dbbt, sizeof(struct mx28_nand_dbbt));
415 static int mx28_nand_write_firmware(struct mx28_nand_fcb *fcb, int infd,
420 uint32_t offset1, offset2;
422 size = lseek(infd, 0, SEEK_END);
423 lseek(infd, 0, SEEK_SET);
425 offset1 = fcb->firmware1_starting_sector * nand_writesize;
426 offset2 = fcb->firmware2_starting_sector * nand_writesize;
428 ret = read(infd, buf + offset1, size);
432 memcpy(buf + offset2, buf + offset1, size);
440 "Usage: mxsboot [ops] <type> <infile> <outfile>\n"
441 "Augment BootStream file with a proper header for i.MX28 boot\n"
443 " <type> type of image:\n"
444 " \"nand\" for NAND image\n"
445 " \"sd\" for SD image\n"
446 " <infile> input file, the u-boot.sb bootstream\n"
447 " <outfile> output file, the bootable image\n"
450 "For NAND boot, these options are accepted:\n"
451 " -w <size> NAND page size\n"
452 " -o <size> NAND OOB size\n"
453 " -e <size> NAND erase size\n"
455 "For SD boot, these options are accepted:\n"
456 " -p <sector> Sector where the SGTL partition starts\n"
460 static int mx28_create_nand_image(int infd, int outfd)
462 struct mx28_nand_fcb *fcb;
463 struct mx28_nand_dbbt *dbbt;
469 size = nand_writesize * 512 + 2 * MAX_BOOTSTREAM_SIZE;
473 printf("Can not allocate output buffer of %d bytes\n", size);
477 memset(buf, 0, size);
479 fcb = mx28_nand_get_fcb(MAX_BOOTSTREAM_SIZE);
481 printf("Unable to compile FCB\n");
485 dbbt = mx28_nand_get_dbbt();
487 printf("Unable to compile DBBT\n");
491 ret = mx28_nand_write_fcb(fcb, buf);
493 printf("Unable to write FCB to buffer\n");
497 ret = mx28_nand_write_dbbt(dbbt, buf);
499 printf("Unable to write DBBT to buffer\n");
503 ret = mx28_nand_write_firmware(fcb, infd, buf);
505 printf("Unable to write firmware to buffer\n");
509 wr_size = write(outfd, buf, size);
510 if (wr_size != size) {
527 static int mx28_create_sd_image(int infd, int outfd)
534 struct mx28_sd_config_block *cb;
536 fsize = lseek(infd, 0, SEEK_END);
537 lseek(infd, 0, SEEK_SET);
538 size = fsize + 4 * 512;
542 printf("Can not allocate output buffer of %d bytes\n", size);
546 ret = read(infd, (uint8_t *)buf + 4 * 512, fsize);
552 cb = (struct mx28_sd_config_block *)buf;
554 cb->signature = 0x00112233;
555 cb->primary_boot_tag = 0x1;
556 cb->secondary_boot_tag = 0x1;
558 cb->drv_info[0].chip_num = 0x0;
559 cb->drv_info[0].drive_type = 0x0;
560 cb->drv_info[0].tag = 0x1;
561 cb->drv_info[0].first_sector_number = sd_sector + 4;
562 cb->drv_info[0].sector_count = (size - 4) / 512;
564 wr_size = write(outfd, buf, size);
565 if (wr_size != size) {
578 int parse_ops(int argc, char **argv)
596 for (i = 1; i < argc; i++) {
597 if (!strncmp(argv[i], "-w", 2))
599 else if (!strncmp(argv[i], "-o", 2))
601 else if (!strncmp(argv[i], "-e", 2))
603 else if (!strncmp(argv[i], "-p", 2))
608 tmp = strtol(argv[++i], &end, 10);
614 if (type == PARAM_WRITE)
615 nand_writesize = tmp;
616 if (type == PARAM_OOB)
618 if (type == PARAM_ERASE)
619 nand_erasesize = tmp;
620 if (type == PARAM_PART)
624 if (strcmp(argv[i], "sd") && strcmp(argv[i], "nand"))
633 int main(int argc, char **argv)
639 offset = parse_ops(argc, argv);
646 infd = open(argv[offset + 1], O_RDONLY);
648 printf("Input BootStream file can not be opened\n");
653 outfd = open(argv[offset + 2], O_CREAT | O_TRUNC | O_WRONLY,
656 printf("Output file can not be created\n");
661 if (!strcmp(argv[offset], "sd"))
662 ret = mx28_create_sd_image(infd, outfd);
663 else if (!strcmp(argv[offset], "nand"))
664 ret = mx28_create_nand_image(infd, outfd);