imximage.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 comand", },
  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. flash_cfg_parms_t *ext_header;
  90. size = imx_hdr->dcd_table.preamble.length;
  91. if (size > (MAX_HW_CFG_SIZE * sizeof(dcd_type_addr_data_t))) {
  92. fprintf(stderr,
  93. "Error: Image corrupt DCD size %d exceed maximum %d\n",
  94. size / sizeof(dcd_type_addr_data_t), MAX_HW_CFG_SIZE);
  95. exit(EXIT_FAILURE);
  96. }
  97. ext_header = (flash_cfg_parms_t *) ((uint32_t)&imx_hdr->dcd_table +
  98. sizeof(dcd_preamble_t) + size);
  99. printf("Image Type: Freescale IMX Boot Image\n");
  100. printf("Data Size: ");
  101. genimg_print_size(ext_header->length);
  102. printf("Load Address: %08x\n", (unsigned int)hdr->app_dest_ptr);
  103. printf("Entry Point: %08x\n", (unsigned int)hdr->app_code_jump_vector);
  104. }
  105. static uint32_t imximage_parse_cfg_file(struct imx_header *imxhdr, char *name)
  106. {
  107. FILE *fd = NULL;
  108. char *line = NULL;
  109. char *token, *saveptr1, *saveptr2;
  110. int lineno = 0;
  111. int fld, value;
  112. uint32_t len;
  113. int dcd_len = 0;
  114. dcd_t *dcd = &imxhdr->dcd_table;
  115. int32_t cmd;
  116. fd = fopen(name, "r");
  117. if (fd == 0) {
  118. fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
  119. exit(EXIT_FAILURE);
  120. }
  121. /* Very simple parsing, line starting with # are comments
  122. * and are dropped
  123. */
  124. while ((getline(&line, &len, fd)) > 0) {
  125. lineno++;
  126. token = strtok_r(line, "\r\n", &saveptr1);
  127. if (token == NULL)
  128. continue;
  129. /* Check inside the single line */
  130. for (fld = CFG_COMMAND, cmd = CMD_INVALID,
  131. line = token; ; line = NULL, fld++) {
  132. token = strtok_r(line, " \t", &saveptr2);
  133. if (token == NULL)
  134. break;
  135. /* Drop all text starting with '#' as comments */
  136. if (token[0] == '#')
  137. break;
  138. /* parse all fields in a single line */
  139. switch (fld) {
  140. case CFG_COMMAND:
  141. cmd = get_table_entry_id(imximage_cmds,
  142. "imximage commands", token);
  143. if (cmd < 0) {
  144. fprintf(stderr,
  145. "Error: %s[%d] - "
  146. "Invalid command (%s)\n",
  147. name, lineno, token);
  148. exit(EXIT_FAILURE);
  149. }
  150. break;
  151. case CFG_REG_SIZE:
  152. switch (cmd) {
  153. case CMD_BOOT_FROM:
  154. /* Get flash header offset */
  155. imxhdr->flash_offset =
  156. get_table_entry_id(
  157. imximage_bootops,
  158. "imximage boot option",
  159. token);
  160. if (imxhdr->flash_offset == -1) {
  161. fprintf(stderr,
  162. "Error: %s[%d] -"
  163. "Invalid boot device"
  164. "(%s)\n",
  165. name, lineno, token);
  166. exit(EXIT_FAILURE);
  167. }
  168. break;
  169. case CMD_DATA:
  170. value = get_cfg_value(token,
  171. name, lineno);
  172. /* Byte, halfword, word */
  173. if ((value != 1) &&
  174. (value != 2) && (value != 4)) {
  175. fprintf(stderr,
  176. "Error: %s[%d] - "
  177. "Invalid register size "
  178. "(%d)\n",
  179. name, lineno, value);
  180. exit(EXIT_FAILURE);
  181. }
  182. dcd->addr_data[dcd_len].type = value;
  183. break;
  184. }
  185. case CFG_REG_ADDRESS:
  186. if (cmd == CMD_DATA)
  187. dcd->addr_data[dcd_len].addr =
  188. get_cfg_value(token,
  189. name, lineno);
  190. break;
  191. case CFG_REG_VALUE:
  192. if (cmd == CMD_DATA) {
  193. dcd->addr_data[dcd_len].value =
  194. get_cfg_value(token,
  195. name, lineno);
  196. dcd_len++;
  197. }
  198. break;
  199. }
  200. }
  201. if (dcd_len > MAX_HW_CFG_SIZE) {
  202. fprintf(stderr,
  203. "Error: %s[%d] -"
  204. "DCD table exceeds maximum size(%d)\n",
  205. name, lineno, MAX_HW_CFG_SIZE);
  206. }
  207. }
  208. dcd->preamble.barker = DCD_BARKER;
  209. dcd->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t);
  210. fclose(fd);
  211. return dcd->preamble.length;
  212. }
  213. static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
  214. struct mkimage_params *params)
  215. {
  216. struct imx_header *hdr = (struct imx_header *)ptr;
  217. flash_header_t *fhdr = &hdr->fhdr;
  218. int dcd_len;
  219. flash_cfg_parms_t *ext_header;
  220. uint32_t base_offset;
  221. /* Set default offset */
  222. hdr->flash_offset = FLASH_OFFSET_STANDARD;
  223. /* Set magic number */
  224. fhdr->app_code_barker = APP_CODE_BARKER;
  225. /* Parse dcd configuration file */
  226. dcd_len = imximage_parse_cfg_file(hdr, params->imagename);
  227. fhdr->app_dest_ptr = params->addr;
  228. fhdr->app_dest_ptr = params->ep - hdr->flash_offset -
  229. sizeof(struct imx_header);
  230. fhdr->app_code_jump_vector = params->ep;
  231. base_offset = fhdr->app_dest_ptr + hdr->flash_offset ;
  232. fhdr->dcd_ptr_ptr = (uint32_t) ((uint32_t)&fhdr->dcd_ptr -
  233. (uint32_t)&fhdr->app_code_jump_vector) + base_offset ;
  234. fhdr->dcd_ptr = base_offset +
  235. ((uint32_t)&hdr->dcd_table -
  236. (uint32_t)&hdr->fhdr);
  237. /* The external flash header must be at the end of the DCD table */
  238. ext_header = (flash_cfg_parms_t *) ((uint32_t)&hdr->dcd_table +
  239. dcd_len +
  240. sizeof(dcd_preamble_t));
  241. ext_header->length = sbuf->st_size +
  242. hdr->flash_offset +
  243. sizeof(struct imx_header);
  244. /* Security feature are not supported */
  245. fhdr->app_code_csf = 0;
  246. fhdr->super_root_key = NULL;
  247. }
  248. int imximage_check_params(struct mkimage_params *params)
  249. {
  250. if (!params)
  251. return CFG_INVALID;
  252. if (!strlen(params->imagename)) {
  253. fprintf(stderr, "Error: %s - Configuration file not specified, "
  254. "it is needed for imximage generation\n",
  255. params->cmdname);
  256. return CFG_INVALID;
  257. }
  258. /*
  259. * Check parameters:
  260. * XIP is not allowed and verify that incompatible
  261. * parameters are not sent at the same time
  262. * For example, if list is required a data image must not be provided
  263. */
  264. return (params->dflag && (params->fflag || params->lflag)) ||
  265. (params->fflag && (params->dflag || params->lflag)) ||
  266. (params->lflag && (params->dflag || params->fflag)) ||
  267. (params->xflag) || !(strlen(params->imagename));
  268. }
  269. /*
  270. * imximage parameters
  271. */
  272. static struct image_type_params imximage_params = {
  273. .name = "Freescale i.MX 51 Boot Image support",
  274. .header_size = sizeof(struct imx_header),
  275. .hdr = (void *)&imximage_header,
  276. .check_image_type = imximage_check_image_types,
  277. .verify_header = imximage_verify_header,
  278. .print_header = imximage_print_header,
  279. .set_header = imximage_set_header,
  280. .check_params = imximage_check_params,
  281. };
  282. void init_imx_image_type(void)
  283. {
  284. mkimage_register(&imximage_params);
  285. }