Marek Vasut | c2cf420 | 2011-11-08 23:18:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Freescale i.MX28 image generator |
| 3 | * |
| 4 | * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com> |
| 5 | * on behalf of DENX Software Engineering GmbH |
| 6 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program 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., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | #include <fcntl.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #include "compiler.h" |
| 32 | |
| 33 | /* |
| 34 | * Default BCB layout. |
| 35 | * |
| 36 | * TWEAK this if you have blown any OCOTP fuses. |
| 37 | */ |
| 38 | #define STRIDE_PAGES 64 |
| 39 | #define STRIDE_COUNT 4 |
| 40 | |
| 41 | /* |
| 42 | * Layout for 256Mb big NAND with 2048b page size, 64b OOB size and |
| 43 | * 128kb erase size. |
| 44 | * |
| 45 | * TWEAK this if you have different kind of NAND chip. |
| 46 | */ |
| 47 | uint32_t nand_writesize = 2048; |
| 48 | uint32_t nand_oobsize = 64; |
| 49 | uint32_t nand_erasesize = 128 * 1024; |
| 50 | |
| 51 | /* |
| 52 | * Sector on which the SigmaTel boot partition (0x53) starts. |
| 53 | */ |
| 54 | uint32_t sd_sector = 2048; |
| 55 | |
| 56 | /* |
| 57 | * Each of the U-Boot bootstreams is at maximum 1MB big. |
| 58 | * |
| 59 | * TWEAK this if, for some wild reason, you need to boot bigger image. |
| 60 | */ |
| 61 | #define MAX_BOOTSTREAM_SIZE (1 * 1024 * 1024) |
| 62 | |
| 63 | /* i.MX28 NAND controller-specific constants. DO NOT TWEAK! */ |
| 64 | #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4 |
| 65 | #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE 512 |
| 66 | #define MXS_NAND_METADATA_SIZE 10 |
| 67 | #define MXS_NAND_COMMAND_BUFFER_SIZE 32 |
| 68 | |
| 69 | struct mx28_nand_fcb { |
| 70 | uint32_t checksum; |
| 71 | uint32_t fingerprint; |
| 72 | uint32_t version; |
| 73 | struct { |
| 74 | uint8_t data_setup; |
| 75 | uint8_t data_hold; |
| 76 | uint8_t address_setup; |
| 77 | uint8_t dsample_time; |
| 78 | uint8_t nand_timing_state; |
| 79 | uint8_t rea; |
| 80 | uint8_t rloh; |
| 81 | uint8_t rhoh; |
| 82 | } timing; |
| 83 | uint32_t page_data_size; |
| 84 | uint32_t total_page_size; |
| 85 | uint32_t sectors_per_block; |
| 86 | uint32_t number_of_nands; /* Ignored */ |
| 87 | uint32_t total_internal_die; /* Ignored */ |
| 88 | uint32_t cell_type; /* Ignored */ |
| 89 | uint32_t ecc_block_n_ecc_type; |
| 90 | uint32_t ecc_block_0_size; |
| 91 | uint32_t ecc_block_n_size; |
| 92 | uint32_t ecc_block_0_ecc_type; |
| 93 | uint32_t metadata_bytes; |
| 94 | uint32_t num_ecc_blocks_per_page; |
| 95 | uint32_t ecc_block_n_ecc_level_sdk; /* Ignored */ |
| 96 | uint32_t ecc_block_0_size_sdk; /* Ignored */ |
| 97 | uint32_t ecc_block_n_size_sdk; /* Ignored */ |
| 98 | uint32_t ecc_block_0_ecc_level_sdk; /* Ignored */ |
| 99 | uint32_t num_ecc_blocks_per_page_sdk; /* Ignored */ |
| 100 | uint32_t metadata_bytes_sdk; /* Ignored */ |
| 101 | uint32_t erase_threshold; |
| 102 | uint32_t boot_patch; |
| 103 | uint32_t patch_sectors; |
| 104 | uint32_t firmware1_starting_sector; |
| 105 | uint32_t firmware2_starting_sector; |
| 106 | uint32_t sectors_in_firmware1; |
| 107 | uint32_t sectors_in_firmware2; |
| 108 | uint32_t dbbt_search_area_start_address; |
| 109 | uint32_t badblock_marker_byte; |
| 110 | uint32_t badblock_marker_start_bit; |
| 111 | uint32_t bb_marker_physical_offset; |
| 112 | }; |
| 113 | |
| 114 | struct mx28_nand_dbbt { |
| 115 | uint32_t checksum; |
| 116 | uint32_t fingerprint; |
| 117 | uint32_t version; |
| 118 | uint32_t number_bb; |
| 119 | uint32_t number_2k_pages_bb; |
| 120 | }; |
| 121 | |
| 122 | struct mx28_nand_bbt { |
| 123 | uint32_t nand; |
| 124 | uint32_t number_bb; |
| 125 | uint32_t badblock[510]; |
| 126 | }; |
| 127 | |
| 128 | struct mx28_sd_drive_info { |
| 129 | uint32_t chip_num; |
| 130 | uint32_t drive_type; |
| 131 | uint32_t tag; |
| 132 | uint32_t first_sector_number; |
| 133 | uint32_t sector_count; |
| 134 | }; |
| 135 | |
| 136 | struct mx28_sd_config_block { |
| 137 | uint32_t signature; |
| 138 | uint32_t primary_boot_tag; |
| 139 | uint32_t secondary_boot_tag; |
| 140 | uint32_t num_copies; |
| 141 | struct mx28_sd_drive_info drv_info[1]; |
| 142 | }; |
| 143 | |
| 144 | static inline uint32_t mx28_nand_ecc_size_in_bits(uint32_t ecc_strength) |
| 145 | { |
| 146 | return ecc_strength * 13; |
| 147 | } |
| 148 | |
| 149 | static inline uint32_t mx28_nand_get_ecc_strength(uint32_t page_data_size, |
| 150 | uint32_t page_oob_size) |
| 151 | { |
| 152 | if (page_data_size == 2048) |
| 153 | return 8; |
| 154 | |
| 155 | if (page_data_size == 4096) { |
| 156 | if (page_oob_size == 128) |
| 157 | return 8; |
| 158 | |
| 159 | if (page_oob_size == 218) |
| 160 | return 16; |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static inline uint32_t mx28_nand_get_mark_offset(uint32_t page_data_size, |
| 167 | uint32_t ecc_strength) |
| 168 | { |
| 169 | uint32_t chunk_data_size_in_bits; |
| 170 | uint32_t chunk_ecc_size_in_bits; |
| 171 | uint32_t chunk_total_size_in_bits; |
| 172 | uint32_t block_mark_chunk_number; |
| 173 | uint32_t block_mark_chunk_bit_offset; |
| 174 | uint32_t block_mark_bit_offset; |
| 175 | |
| 176 | chunk_data_size_in_bits = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 8; |
| 177 | chunk_ecc_size_in_bits = mx28_nand_ecc_size_in_bits(ecc_strength); |
| 178 | |
| 179 | chunk_total_size_in_bits = |
| 180 | chunk_data_size_in_bits + chunk_ecc_size_in_bits; |
| 181 | |
| 182 | /* Compute the bit offset of the block mark within the physical page. */ |
| 183 | block_mark_bit_offset = page_data_size * 8; |
| 184 | |
| 185 | /* Subtract the metadata bits. */ |
| 186 | block_mark_bit_offset -= MXS_NAND_METADATA_SIZE * 8; |
| 187 | |
| 188 | /* |
| 189 | * Compute the chunk number (starting at zero) in which the block mark |
| 190 | * appears. |
| 191 | */ |
| 192 | block_mark_chunk_number = |
| 193 | block_mark_bit_offset / chunk_total_size_in_bits; |
| 194 | |
| 195 | /* |
| 196 | * Compute the bit offset of the block mark within its chunk, and |
| 197 | * validate it. |
| 198 | */ |
| 199 | block_mark_chunk_bit_offset = block_mark_bit_offset - |
| 200 | (block_mark_chunk_number * chunk_total_size_in_bits); |
| 201 | |
| 202 | if (block_mark_chunk_bit_offset > chunk_data_size_in_bits) |
| 203 | return 1; |
| 204 | |
| 205 | /* |
| 206 | * Now that we know the chunk number in which the block mark appears, |
| 207 | * we can subtract all the ECC bits that appear before it. |
| 208 | */ |
| 209 | block_mark_bit_offset -= |
| 210 | block_mark_chunk_number * chunk_ecc_size_in_bits; |
| 211 | |
| 212 | return block_mark_bit_offset; |
| 213 | } |
| 214 | |
| 215 | static inline uint32_t mx28_nand_mark_byte_offset(void) |
| 216 | { |
| 217 | uint32_t ecc_strength; |
| 218 | ecc_strength = mx28_nand_get_ecc_strength(nand_writesize, nand_oobsize); |
| 219 | return mx28_nand_get_mark_offset(nand_writesize, ecc_strength) >> 3; |
| 220 | } |
| 221 | |
| 222 | static inline uint32_t mx28_nand_mark_bit_offset(void) |
| 223 | { |
| 224 | uint32_t ecc_strength; |
| 225 | ecc_strength = mx28_nand_get_ecc_strength(nand_writesize, nand_oobsize); |
| 226 | return mx28_nand_get_mark_offset(nand_writesize, ecc_strength) & 0x7; |
| 227 | } |
| 228 | |
| 229 | static uint32_t mx28_nand_block_csum(uint8_t *block, uint32_t size) |
| 230 | { |
| 231 | uint32_t csum = 0; |
| 232 | int i; |
| 233 | |
| 234 | for (i = 0; i < size; i++) |
| 235 | csum += block[i]; |
| 236 | |
| 237 | return csum ^ 0xffffffff; |
| 238 | } |
| 239 | |
| 240 | static struct mx28_nand_fcb *mx28_nand_get_fcb(uint32_t size) |
| 241 | { |
| 242 | struct mx28_nand_fcb *fcb; |
| 243 | uint32_t bcb_size_bytes; |
| 244 | uint32_t stride_size_bytes; |
| 245 | uint32_t bootstream_size_pages; |
| 246 | uint32_t fw1_start_page; |
| 247 | uint32_t fw2_start_page; |
| 248 | |
| 249 | fcb = malloc(nand_writesize); |
| 250 | if (!fcb) { |
| 251 | printf("MX28 NAND: Unable to allocate FCB\n"); |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | memset(fcb, 0, nand_writesize); |
| 256 | |
| 257 | fcb->fingerprint = 0x20424346; |
| 258 | fcb->version = 0x01000000; |
| 259 | |
| 260 | /* |
| 261 | * FIXME: These here are default values as found in kobs-ng. We should |
| 262 | * probably retrieve the data from NAND or something. |
| 263 | */ |
| 264 | fcb->timing.data_setup = 80; |
| 265 | fcb->timing.data_hold = 60; |
| 266 | fcb->timing.address_setup = 25; |
| 267 | fcb->timing.dsample_time = 6; |
| 268 | |
| 269 | fcb->page_data_size = nand_writesize; |
| 270 | fcb->total_page_size = nand_writesize + nand_oobsize; |
| 271 | fcb->sectors_per_block = nand_erasesize / nand_writesize; |
| 272 | |
| 273 | fcb->num_ecc_blocks_per_page = (nand_writesize / 512) - 1; |
| 274 | fcb->ecc_block_0_size = 512; |
| 275 | fcb->ecc_block_n_size = 512; |
| 276 | fcb->metadata_bytes = 10; |
| 277 | |
| 278 | if (nand_writesize == 2048) { |
| 279 | fcb->ecc_block_n_ecc_type = 4; |
| 280 | fcb->ecc_block_0_ecc_type = 4; |
| 281 | } else if (nand_writesize == 4096) { |
| 282 | if (nand_oobsize == 128) { |
| 283 | fcb->ecc_block_n_ecc_type = 4; |
| 284 | fcb->ecc_block_0_ecc_type = 4; |
| 285 | } else if (nand_oobsize == 218) { |
| 286 | fcb->ecc_block_n_ecc_type = 8; |
| 287 | fcb->ecc_block_0_ecc_type = 8; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if (fcb->ecc_block_n_ecc_type == 0) { |
| 292 | printf("MX28 NAND: Unsupported NAND geometry\n"); |
| 293 | goto err; |
| 294 | } |
| 295 | |
| 296 | fcb->boot_patch = 0; |
| 297 | fcb->patch_sectors = 0; |
| 298 | |
| 299 | fcb->badblock_marker_byte = mx28_nand_mark_byte_offset(); |
| 300 | fcb->badblock_marker_start_bit = mx28_nand_mark_bit_offset(); |
| 301 | fcb->bb_marker_physical_offset = nand_writesize; |
| 302 | |
| 303 | stride_size_bytes = STRIDE_PAGES * nand_writesize; |
| 304 | bcb_size_bytes = stride_size_bytes * STRIDE_COUNT; |
| 305 | |
| 306 | bootstream_size_pages = (size + (nand_writesize - 1)) / |
| 307 | nand_writesize; |
| 308 | |
| 309 | fw1_start_page = 2 * bcb_size_bytes / nand_writesize; |
| 310 | fw2_start_page = (2 * bcb_size_bytes + MAX_BOOTSTREAM_SIZE) / |
| 311 | nand_writesize; |
| 312 | |
| 313 | fcb->firmware1_starting_sector = fw1_start_page; |
| 314 | fcb->firmware2_starting_sector = fw2_start_page; |
| 315 | fcb->sectors_in_firmware1 = bootstream_size_pages; |
| 316 | fcb->sectors_in_firmware2 = bootstream_size_pages; |
| 317 | |
| 318 | fcb->dbbt_search_area_start_address = STRIDE_PAGES * STRIDE_COUNT; |
| 319 | |
| 320 | return fcb; |
| 321 | |
| 322 | err: |
| 323 | free(fcb); |
| 324 | return NULL; |
| 325 | } |
| 326 | |
| 327 | static struct mx28_nand_dbbt *mx28_nand_get_dbbt(void) |
| 328 | { |
| 329 | struct mx28_nand_dbbt *dbbt; |
| 330 | |
| 331 | dbbt = malloc(nand_writesize); |
| 332 | if (!dbbt) { |
| 333 | printf("MX28 NAND: Unable to allocate DBBT\n"); |
| 334 | return NULL; |
| 335 | } |
| 336 | |
| 337 | memset(dbbt, 0, nand_writesize); |
| 338 | |
| 339 | dbbt->fingerprint = 0x54424244; |
| 340 | dbbt->version = 0x1; |
| 341 | |
| 342 | return dbbt; |
| 343 | } |
| 344 | |
| 345 | static inline uint8_t mx28_nand_parity_13_8(const uint8_t b) |
| 346 | { |
| 347 | uint32_t parity = 0, tmp; |
| 348 | |
| 349 | tmp = ((b >> 6) ^ (b >> 5) ^ (b >> 3) ^ (b >> 2)) & 1; |
| 350 | parity |= tmp << 0; |
| 351 | |
| 352 | tmp = ((b >> 7) ^ (b >> 5) ^ (b >> 4) ^ (b >> 2) ^ (b >> 1)) & 1; |
| 353 | parity |= tmp << 1; |
| 354 | |
| 355 | tmp = ((b >> 7) ^ (b >> 6) ^ (b >> 5) ^ (b >> 1) ^ (b >> 0)) & 1; |
| 356 | parity |= tmp << 2; |
| 357 | |
| 358 | tmp = ((b >> 7) ^ (b >> 4) ^ (b >> 3) ^ (b >> 0)) & 1; |
| 359 | parity |= tmp << 3; |
| 360 | |
| 361 | tmp = ((b >> 6) ^ (b >> 4) ^ (b >> 3) ^ |
| 362 | (b >> 2) ^ (b >> 1) ^ (b >> 0)) & 1; |
| 363 | parity |= tmp << 4; |
| 364 | |
| 365 | return parity; |
| 366 | } |
| 367 | |
| 368 | static uint8_t *mx28_nand_fcb_block(struct mx28_nand_fcb *fcb) |
| 369 | { |
| 370 | uint8_t *block; |
| 371 | uint8_t *ecc; |
| 372 | int i; |
| 373 | |
| 374 | block = malloc(nand_writesize + nand_oobsize); |
| 375 | if (!block) { |
| 376 | printf("MX28 NAND: Unable to allocate FCB block\n"); |
| 377 | return NULL; |
| 378 | } |
| 379 | |
| 380 | memset(block, 0, nand_writesize + nand_oobsize); |
| 381 | |
| 382 | /* Update the FCB checksum */ |
| 383 | fcb->checksum = mx28_nand_block_csum(((uint8_t *)fcb) + 4, 508); |
| 384 | |
| 385 | /* Figure 12-11. in iMX28RM, rev. 1, says FCB is at offset 12 */ |
| 386 | memcpy(block + 12, fcb, sizeof(struct mx28_nand_fcb)); |
| 387 | |
| 388 | /* ECC is at offset 12 + 512 */ |
| 389 | ecc = block + 12 + 512; |
| 390 | |
| 391 | /* Compute the ECC parity */ |
| 392 | for (i = 0; i < sizeof(struct mx28_nand_fcb); i++) |
| 393 | ecc[i] = mx28_nand_parity_13_8(block[i + 12]); |
| 394 | |
| 395 | return block; |
| 396 | } |
| 397 | |
| 398 | static int mx28_nand_write_fcb(struct mx28_nand_fcb *fcb, char *buf) |
| 399 | { |
| 400 | uint32_t offset; |
| 401 | uint8_t *fcbblock; |
| 402 | int ret = 0; |
| 403 | int i; |
| 404 | |
| 405 | fcbblock = mx28_nand_fcb_block(fcb); |
| 406 | if (!fcbblock) |
| 407 | return -1; |
| 408 | |
| 409 | for (i = 0; i < STRIDE_PAGES * STRIDE_COUNT; i += STRIDE_PAGES) { |
| 410 | offset = i * nand_writesize; |
| 411 | memcpy(buf + offset, fcbblock, nand_writesize + nand_oobsize); |
| 412 | } |
| 413 | |
| 414 | free(fcbblock); |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | static int mx28_nand_write_dbbt(struct mx28_nand_dbbt *dbbt, char *buf) |
| 419 | { |
| 420 | uint32_t offset; |
| 421 | int i = STRIDE_PAGES * STRIDE_COUNT; |
| 422 | |
| 423 | for (; i < 2 * STRIDE_PAGES * STRIDE_COUNT; i += STRIDE_PAGES) { |
| 424 | offset = i * nand_writesize; |
| 425 | memcpy(buf + offset, dbbt, sizeof(struct mx28_nand_dbbt)); |
| 426 | } |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static int mx28_nand_write_firmware(struct mx28_nand_fcb *fcb, int infd, |
| 432 | char *buf) |
| 433 | { |
| 434 | int ret; |
| 435 | off_t size; |
| 436 | uint32_t offset1, offset2; |
| 437 | |
| 438 | size = lseek(infd, 0, SEEK_END); |
| 439 | lseek(infd, 0, SEEK_SET); |
| 440 | |
| 441 | offset1 = fcb->firmware1_starting_sector * nand_writesize; |
| 442 | offset2 = fcb->firmware2_starting_sector * nand_writesize; |
| 443 | |
| 444 | ret = read(infd, buf + offset1, size); |
| 445 | if (ret != size) |
| 446 | return -1; |
| 447 | |
| 448 | memcpy(buf + offset2, buf + offset1, size); |
| 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | void usage(void) |
| 454 | { |
| 455 | printf( |
| 456 | "Usage: mx28image [ops] <type> <infile> <outfile>\n" |
| 457 | "Augment BootStream file with a proper header for i.MX28 boot\n" |
| 458 | "\n" |
| 459 | " <type> type of image:\n" |
| 460 | " \"nand\" for NAND image\n" |
| 461 | " \"sd\" for SD image\n" |
| 462 | " <infile> input file, the u-boot.sb bootstream\n" |
| 463 | " <outfile> output file, the bootable image\n" |
| 464 | "\n"); |
| 465 | printf( |
| 466 | "For NAND boot, these options are accepted:\n" |
| 467 | " -w <size> NAND page size\n" |
| 468 | " -o <size> NAND OOB size\n" |
| 469 | " -e <size> NAND erase size\n" |
| 470 | "\n" |
| 471 | "For SD boot, these options are accepted:\n" |
| 472 | " -p <sector> Sector where the SGTL partition starts\n" |
| 473 | ); |
| 474 | } |
| 475 | |
| 476 | static int mx28_create_nand_image(int infd, int outfd) |
| 477 | { |
| 478 | struct mx28_nand_fcb *fcb; |
| 479 | struct mx28_nand_dbbt *dbbt; |
| 480 | int ret = -1; |
| 481 | char *buf; |
| 482 | int size; |
| 483 | ssize_t wr_size; |
| 484 | |
| 485 | size = nand_writesize * 512 + 2 * MAX_BOOTSTREAM_SIZE; |
| 486 | |
| 487 | buf = malloc(size); |
| 488 | if (!buf) { |
| 489 | printf("Can not allocate output buffer of %d bytes\n", size); |
| 490 | goto err0; |
| 491 | } |
| 492 | |
| 493 | memset(buf, 0, size); |
| 494 | |
| 495 | fcb = mx28_nand_get_fcb(MAX_BOOTSTREAM_SIZE); |
| 496 | if (!fcb) { |
| 497 | printf("Unable to compile FCB\n"); |
| 498 | goto err1; |
| 499 | } |
| 500 | |
| 501 | dbbt = mx28_nand_get_dbbt(); |
| 502 | if (!dbbt) { |
| 503 | printf("Unable to compile DBBT\n"); |
| 504 | goto err2; |
| 505 | } |
| 506 | |
| 507 | ret = mx28_nand_write_fcb(fcb, buf); |
| 508 | if (ret) { |
| 509 | printf("Unable to write FCB to buffer\n"); |
| 510 | goto err3; |
| 511 | } |
| 512 | |
| 513 | ret = mx28_nand_write_dbbt(dbbt, buf); |
| 514 | if (ret) { |
| 515 | printf("Unable to write DBBT to buffer\n"); |
| 516 | goto err3; |
| 517 | } |
| 518 | |
| 519 | ret = mx28_nand_write_firmware(fcb, infd, buf); |
| 520 | if (ret) { |
| 521 | printf("Unable to write firmware to buffer\n"); |
| 522 | goto err3; |
| 523 | } |
| 524 | |
| 525 | wr_size = write(outfd, buf, size); |
| 526 | if (wr_size != size) { |
| 527 | ret = -1; |
| 528 | goto err3; |
| 529 | } |
| 530 | |
| 531 | ret = 0; |
| 532 | |
| 533 | err3: |
| 534 | free(dbbt); |
| 535 | err2: |
| 536 | free(fcb); |
| 537 | err1: |
| 538 | free(buf); |
| 539 | err0: |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | static int mx28_create_sd_image(int infd, int outfd) |
| 544 | { |
| 545 | int ret = -1; |
| 546 | uint32_t *buf; |
| 547 | int size; |
| 548 | off_t fsize; |
| 549 | ssize_t wr_size; |
| 550 | struct mx28_sd_config_block *cb; |
| 551 | |
| 552 | fsize = lseek(infd, 0, SEEK_END); |
| 553 | lseek(infd, 0, SEEK_SET); |
| 554 | size = fsize + 512; |
| 555 | |
| 556 | buf = malloc(size); |
| 557 | if (!buf) { |
| 558 | printf("Can not allocate output buffer of %d bytes\n", size); |
| 559 | goto err0; |
| 560 | } |
| 561 | |
| 562 | ret = read(infd, (uint8_t *)buf + 512, fsize); |
| 563 | if (ret != fsize) { |
| 564 | ret = -1; |
| 565 | goto err1; |
| 566 | } |
| 567 | |
| 568 | cb = (struct mx28_sd_config_block *)buf; |
| 569 | |
| 570 | cb->signature = 0x00112233; |
| 571 | cb->primary_boot_tag = 0x1; |
| 572 | cb->secondary_boot_tag = 0x1; |
| 573 | cb->num_copies = 1; |
| 574 | cb->drv_info[0].chip_num = 0x0; |
| 575 | cb->drv_info[0].drive_type = 0x0; |
| 576 | cb->drv_info[0].tag = 0x1; |
| 577 | cb->drv_info[0].first_sector_number = sd_sector + 1; |
| 578 | cb->drv_info[0].sector_count = (size - 1) / 512; |
| 579 | |
| 580 | wr_size = write(outfd, buf, size); |
| 581 | if (wr_size != size) { |
| 582 | ret = -1; |
| 583 | goto err1; |
| 584 | } |
| 585 | |
| 586 | ret = 0; |
| 587 | |
| 588 | err1: |
| 589 | free(buf); |
| 590 | err0: |
| 591 | return ret; |
| 592 | } |
| 593 | |
| 594 | int parse_ops(int argc, char **argv) |
| 595 | { |
| 596 | int i; |
| 597 | int tmp; |
| 598 | char *end; |
| 599 | enum param { |
| 600 | PARAM_WRITE, |
| 601 | PARAM_OOB, |
| 602 | PARAM_ERASE, |
| 603 | PARAM_PART, |
| 604 | PARAM_SD, |
| 605 | PARAM_NAND |
| 606 | }; |
| 607 | int type; |
| 608 | |
| 609 | for (i = 1; i < argc; i++) { |
| 610 | if (!strncmp(argv[i], "-w", 2)) |
| 611 | type = PARAM_WRITE; |
| 612 | else if (!strncmp(argv[i], "-o", 2)) |
| 613 | type = PARAM_OOB; |
| 614 | else if (!strncmp(argv[i], "-e", 2)) |
| 615 | type = PARAM_ERASE; |
| 616 | else if (!strncmp(argv[i], "-p", 2)) |
| 617 | type = PARAM_PART; |
| 618 | else /* SD/MMC */ |
| 619 | break; |
| 620 | |
| 621 | tmp = strtol(argv[++i], &end, 10); |
| 622 | if (tmp % 2) |
| 623 | return -1; |
| 624 | if (tmp <= 0) |
| 625 | return -1; |
| 626 | |
| 627 | if (type == PARAM_WRITE) |
| 628 | nand_writesize = tmp; |
| 629 | if (type == PARAM_OOB) |
| 630 | nand_oobsize = tmp; |
| 631 | if (type == PARAM_ERASE) |
| 632 | nand_erasesize = tmp; |
| 633 | if (type == PARAM_PART) |
| 634 | sd_sector = tmp; |
| 635 | } |
| 636 | |
| 637 | if (strcmp(argv[i], "sd") && strcmp(argv[i], "nand")) |
| 638 | return -1; |
| 639 | |
| 640 | if (i + 3 != argc) |
| 641 | return -1; |
| 642 | |
| 643 | return i; |
| 644 | } |
| 645 | |
| 646 | int main(int argc, char **argv) |
| 647 | { |
| 648 | int infd, outfd; |
| 649 | int ret = 0; |
| 650 | int offset; |
| 651 | |
| 652 | offset = parse_ops(argc, argv); |
| 653 | if (offset < 0) { |
| 654 | usage(); |
| 655 | ret = 1; |
| 656 | goto err1; |
| 657 | } |
| 658 | |
| 659 | infd = open(argv[offset + 1], O_RDONLY); |
| 660 | if (infd < 0) { |
| 661 | printf("Input BootStream file can not be opened\n"); |
| 662 | ret = 2; |
| 663 | goto err1; |
| 664 | } |
| 665 | |
| 666 | outfd = open(argv[offset + 2], O_CREAT | O_TRUNC | O_WRONLY, |
| 667 | S_IRUSR | S_IWUSR); |
| 668 | if (outfd < 0) { |
| 669 | printf("Output file can not be created\n"); |
| 670 | ret = 3; |
| 671 | goto err2; |
| 672 | } |
| 673 | |
| 674 | if (!strcmp(argv[offset], "sd")) |
| 675 | ret = mx28_create_sd_image(infd, outfd); |
| 676 | else if (!strcmp(argv[offset], "nand")) |
| 677 | ret = mx28_create_nand_image(infd, outfd); |
| 678 | |
| 679 | close(outfd); |
| 680 | err2: |
| 681 | close(infd); |
| 682 | err1: |
| 683 | return ret; |
| 684 | } |