imximage.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * (C) Copyright 2009
  3. * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
  4. *
  5. * (C) Copyright 2008
  6. * Marvell Semiconductor <www.marvell.com>
  7. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. /* Required to obtain the getline prototype from stdio.h */
  28. #define _GNU_SOURCE
  29. #include "mkimage.h"
  30. #include <image.h>
  31. #include "imximage.h"
  32. /*
  33. * Supported commands for configuration file
  34. */
  35. static table_entry_t imximage_cmds[] = {
  36. {CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
  37. {CMD_DATA, "DATA", "Reg Write Data", },
  38. {-1, "", "", },
  39. };
  40. /*
  41. * Supported Boot options for configuration file
  42. * this is needed to set the correct flash offset
  43. */
  44. static table_entry_t imximage_bootops[] = {
  45. {FLASH_OFFSET_SPI, "spi", "SPI Flash", },
  46. {FLASH_OFFSET_NAND, "nand", "NAND Flash", },
  47. {FLASH_OFFSET_SD, "sd", "SD Card", },
  48. {FLASH_OFFSET_ONENAND, "onenand", "OneNAND Flash",},
  49. {-1, "", "Invalid", },
  50. };
  51. static struct imx_header imximage_header;
  52. static uint32_t get_cfg_value(char *token, char *name, int linenr)
  53. {
  54. char *endptr;
  55. uint32_t value;
  56. errno = 0;
  57. value = strtoul(token, &endptr, 16);
  58. if (errno || (token == endptr)) {
  59. fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
  60. name, linenr, token);
  61. exit(EXIT_FAILURE);
  62. }
  63. return value;
  64. }
  65. static int imximage_check_image_types(uint8_t type)
  66. {
  67. if (type == IH_TYPE_IMXIMAGE)
  68. return EXIT_SUCCESS;
  69. else
  70. return EXIT_FAILURE;
  71. }
  72. static int imximage_verify_header(unsigned char *ptr, int image_size,
  73. struct mkimage_params *params)
  74. {
  75. struct imx_header *imx_hdr = (struct imx_header *) ptr;
  76. flash_header_t *hdr = &imx_hdr->fhdr;
  77. /* Only a few checks can be done: search for magic numbers */
  78. if (hdr->app_code_barker != APP_CODE_BARKER)
  79. return -FDT_ERR_BADSTRUCTURE;
  80. if (imx_hdr->dcd_table.preamble.barker != DCD_BARKER)
  81. return -FDT_ERR_BADSTRUCTURE;
  82. return 0;
  83. }
  84. static void imximage_print_header(const void *ptr)
  85. {
  86. struct imx_header *imx_hdr = (struct imx_header *) ptr;
  87. flash_header_t *hdr = &imx_hdr->fhdr;
  88. uint32_t size;
  89. uint32_t length;
  90. dcd_t *dcd = &imx_hdr->dcd_table;
  91. size = imx_hdr->dcd_table.preamble.length;
  92. if (size > (MAX_HW_CFG_SIZE * sizeof(dcd_type_addr_data_t))) {
  93. fprintf(stderr,
  94. "Error: Image corrupt DCD size %d exceed maximum %d\n",
  95. (uint32_t)(size / sizeof(dcd_type_addr_data_t)),
  96. MAX_HW_CFG_SIZE);
  97. exit(EXIT_FAILURE);
  98. }
  99. length = dcd->preamble.length / sizeof(dcd_type_addr_data_t);
  100. printf("Image Type: Freescale IMX Boot Image\n");
  101. printf("Data Size: ");
  102. genimg_print_size(dcd->addr_data[length].type);
  103. printf("Load Address: %08x\n", (unsigned int)hdr->app_dest_ptr);
  104. printf("Entry Point: %08x\n", (unsigned int)hdr->app_code_jump_vector);
  105. }
  106. static uint32_t imximage_parse_cfg_file(struct imx_header *imxhdr, char *name)
  107. {
  108. FILE *fd = NULL;
  109. char *line = NULL;
  110. char *token, *saveptr1, *saveptr2;
  111. int lineno = 0;
  112. int fld, value;
  113. size_t len;
  114. int dcd_len = 0;
  115. dcd_t *dcd = &imxhdr->dcd_table;
  116. int32_t cmd;
  117. fd = fopen(name, "r");
  118. if (fd == 0) {
  119. fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
  120. exit(EXIT_FAILURE);
  121. }
  122. /* Very simple parsing, line starting with # are comments
  123. * and are dropped
  124. */
  125. while ((getline(&line, &len, fd)) > 0) {
  126. lineno++;
  127. token = strtok_r(line, "\r\n", &saveptr1);
  128. if (token == NULL)
  129. continue;
  130. /* Check inside the single line */
  131. for (fld = CFG_COMMAND, cmd = CMD_INVALID,
  132. line = token; ; line = NULL, fld++) {
  133. token = strtok_r(line, " \t", &saveptr2);
  134. if (token == NULL)
  135. break;
  136. /* Drop all text starting with '#' as comments */
  137. if (token[0] == '#')
  138. break;
  139. /* parse all fields in a single line */
  140. switch (fld) {
  141. case CFG_COMMAND:
  142. cmd = get_table_entry_id(imximage_cmds,
  143. "imximage commands", token);
  144. if (cmd < 0) {
  145. fprintf(stderr,
  146. "Error: %s[%d] - "
  147. "Invalid command (%s)\n",
  148. name, lineno, token);
  149. exit(EXIT_FAILURE);
  150. }
  151. break;
  152. case CFG_REG_SIZE:
  153. switch (cmd) {
  154. case CMD_BOOT_FROM:
  155. /* Get flash header offset */
  156. imxhdr->flash_offset =
  157. get_table_entry_id(
  158. imximage_bootops,
  159. "imximage boot option",
  160. token);
  161. if (imxhdr->flash_offset == -1) {
  162. fprintf(stderr,
  163. "Error: %s[%d] -"
  164. "Invalid boot device"
  165. "(%s)\n",
  166. name, lineno, token);
  167. exit(EXIT_FAILURE);
  168. }
  169. break;
  170. case CMD_DATA:
  171. value = get_cfg_value(token,
  172. name, lineno);
  173. /* Byte, halfword, word */
  174. if ((value != 1) &&
  175. (value != 2) && (value != 4)) {
  176. fprintf(stderr,
  177. "Error: %s[%d] - "
  178. "Invalid register size "
  179. "(%d)\n",
  180. name, lineno, value);
  181. exit(EXIT_FAILURE);
  182. }
  183. dcd->addr_data[dcd_len].type = value;
  184. break;
  185. }
  186. case CFG_REG_ADDRESS:
  187. if (cmd == CMD_DATA)
  188. dcd->addr_data[dcd_len].addr =
  189. get_cfg_value(token,
  190. name, lineno);
  191. break;
  192. case CFG_REG_VALUE:
  193. if (cmd == CMD_DATA) {
  194. dcd->addr_data[dcd_len].value =
  195. get_cfg_value(token,
  196. name, lineno);
  197. dcd_len++;
  198. }
  199. break;
  200. }
  201. }
  202. if (dcd_len > MAX_HW_CFG_SIZE) {
  203. fprintf(stderr,
  204. "Error: %s[%d] -"
  205. "DCD table exceeds maximum size(%d)\n",
  206. name, lineno, MAX_HW_CFG_SIZE);
  207. }
  208. }
  209. dcd->preamble.barker = DCD_BARKER;
  210. dcd->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t);
  211. fclose(fd);
  212. return dcd_len;
  213. }
  214. static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
  215. struct mkimage_params *params)
  216. {
  217. struct imx_header *hdr = (struct imx_header *)ptr;
  218. flash_header_t *fhdr = &hdr->fhdr;
  219. int dcd_len;
  220. dcd_t *dcd = &hdr->dcd_table;
  221. uint32_t base_offset;
  222. /* Set default offset */
  223. hdr->flash_offset = FLASH_OFFSET_STANDARD;
  224. /* Set magic number */
  225. fhdr->app_code_barker = APP_CODE_BARKER;
  226. /* Parse dcd configuration file */
  227. dcd_len = imximage_parse_cfg_file(hdr, params->imagename);
  228. fhdr->app_dest_ptr = params->addr;
  229. fhdr->app_dest_ptr = params->ep - hdr->flash_offset -
  230. sizeof(struct imx_header);
  231. fhdr->app_code_jump_vector = params->ep;
  232. base_offset = fhdr->app_dest_ptr + hdr->flash_offset ;
  233. fhdr->dcd_ptr_ptr = (uint32_t) (offsetof(flash_header_t, dcd_ptr) -
  234. offsetof(flash_header_t, app_code_jump_vector) +
  235. base_offset);
  236. fhdr->dcd_ptr = base_offset +
  237. offsetof(struct imx_header, dcd_table);
  238. /* The external flash header must be at the end of the DCD table */
  239. dcd->addr_data[dcd_len].type = sbuf->st_size +
  240. hdr->flash_offset +
  241. sizeof(struct imx_header);
  242. /* Security feature are not supported */
  243. fhdr->app_code_csf = 0;
  244. fhdr->super_root_key = 0;
  245. }
  246. int imximage_check_params(struct mkimage_params *params)
  247. {
  248. if (!params)
  249. return CFG_INVALID;
  250. if (!strlen(params->imagename)) {
  251. fprintf(stderr, "Error: %s - Configuration file not specified, "
  252. "it is needed for imximage generation\n",
  253. params->cmdname);
  254. return CFG_INVALID;
  255. }
  256. /*
  257. * Check parameters:
  258. * XIP is not allowed and verify that incompatible
  259. * parameters are not sent at the same time
  260. * For example, if list is required a data image must not be provided
  261. */
  262. return (params->dflag && (params->fflag || params->lflag)) ||
  263. (params->fflag && (params->dflag || params->lflag)) ||
  264. (params->lflag && (params->dflag || params->fflag)) ||
  265. (params->xflag) || !(strlen(params->imagename));
  266. }
  267. /*
  268. * imximage parameters
  269. */
  270. static struct image_type_params imximage_params = {
  271. .name = "Freescale i.MX 51 Boot Image support",
  272. .header_size = sizeof(struct imx_header),
  273. .hdr = (void *)&imximage_header,
  274. .check_image_type = imximage_check_image_types,
  275. .verify_header = imximage_verify_header,
  276. .print_header = imximage_print_header,
  277. .set_header = imximage_set_header,
  278. .check_params = imximage_check_params,
  279. };
  280. void init_imx_image_type(void)
  281. {
  282. mkimage_register(&imximage_params);
  283. }