ublimage.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * (C) Copyright 2011
  3. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  4. *
  5. * Based on:
  6. * (C) Copyright 2009
  7. * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
  8. *
  9. * (C) Copyright 2008
  10. * Marvell Semiconductor <www.marvell.com>
  11. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  12. *
  13. * See file CREDITS for list of people who contributed to this
  14. * project.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. */
  31. /* Required to obtain the getline prototype from stdio.h */
  32. #define _GNU_SOURCE
  33. #include "mkimage.h"
  34. #include <image.h>
  35. #include "ublimage.h"
  36. /*
  37. * Supported commands for configuration file
  38. */
  39. static table_entry_t ublimage_cmds[] = {
  40. {CMD_BOOT_MODE, "MODE", "UBL special modes", },
  41. {CMD_ENTRY, "ENTRY", "Entry point addr for bootloader", },
  42. {CMD_PAGE, "PAGES",
  43. "number of pages (size of bootloader)", },
  44. {CMD_ST_BLOCK, "START_BLOCK",
  45. "block number where bootloader is present", },
  46. {CMD_ST_PAGE, "START_PAGE",
  47. "page number where bootloader is present", },
  48. {CMD_LD_ADDR, "LD_ADDR",
  49. "load addr", },
  50. {-1, "", "", },
  51. };
  52. /*
  53. * Supported Boot options for configuration file
  54. * this is needed to set the correct flash offset
  55. */
  56. static table_entry_t ublimage_bootops[] = {
  57. {UBL_MAGIC_SAFE, "safe", "Safe boot mode", },
  58. {-1, "", "Invalid", },
  59. };
  60. static struct ubl_header ublimage_header;
  61. static uint32_t get_cfg_value(char *token, char *name, int linenr)
  62. {
  63. char *endptr;
  64. uint32_t value;
  65. errno = 0;
  66. value = strtoul(token, &endptr, 16);
  67. if (errno || (token == endptr)) {
  68. fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
  69. name, linenr, token);
  70. exit(EXIT_FAILURE);
  71. }
  72. return value;
  73. }
  74. static void print_hdr(struct ubl_header *ubl_hdr)
  75. {
  76. printf("Image Type : Davinci UBL Boot Image\n");
  77. printf("UBL magic : %08x\n", ubl_hdr->magic);
  78. printf("Entry Point: %08x\n", ubl_hdr->entry);
  79. printf("nr of pages: %08x\n", ubl_hdr->pages);
  80. printf("start block: %08x\n", ubl_hdr->block);
  81. printf("start page : %08x\n", ubl_hdr->page);
  82. }
  83. static void parse_cfg_cmd(struct ubl_header *ublhdr, int32_t cmd, char *token,
  84. char *name, int lineno, int fld, int dcd_len)
  85. {
  86. static int cmd_ver_first = ~0;
  87. switch (cmd) {
  88. case CMD_BOOT_MODE:
  89. ublhdr->magic = get_table_entry_id(ublimage_bootops,
  90. "ublimage special boot mode", token);
  91. if (ublhdr->magic == -1) {
  92. fprintf(stderr, "Error: %s[%d] -Invalid boot mode"
  93. "(%s)\n", name, lineno, token);
  94. exit(EXIT_FAILURE);
  95. }
  96. ublhdr->magic += UBL_MAGIC_BASE;
  97. if (unlikely(cmd_ver_first != 1))
  98. cmd_ver_first = 0;
  99. break;
  100. case CMD_ENTRY:
  101. ublhdr->entry = get_cfg_value(token, name, lineno);
  102. break;
  103. case CMD_PAGE:
  104. ublhdr->pages = get_cfg_value(token, name, lineno);
  105. break;
  106. case CMD_ST_BLOCK:
  107. ublhdr->block = get_cfg_value(token, name, lineno);
  108. break;
  109. case CMD_ST_PAGE:
  110. ublhdr->page = get_cfg_value(token, name, lineno);
  111. break;
  112. case CMD_LD_ADDR:
  113. ublhdr->pll_m = get_cfg_value(token, name, lineno);
  114. break;
  115. }
  116. }
  117. static void parse_cfg_fld(struct ubl_header *ublhdr, int32_t *cmd,
  118. char *token, char *name, int lineno, int fld, int *dcd_len)
  119. {
  120. switch (fld) {
  121. case CFG_COMMAND:
  122. *cmd = get_table_entry_id(ublimage_cmds,
  123. "ublimage commands", token);
  124. if (*cmd < 0) {
  125. fprintf(stderr, "Error: %s[%d] - Invalid command"
  126. "(%s)\n", name, lineno, token);
  127. exit(EXIT_FAILURE);
  128. }
  129. break;
  130. case CFG_REG_VALUE:
  131. parse_cfg_cmd(ublhdr, *cmd, token, name, lineno, fld, *dcd_len);
  132. break;
  133. default:
  134. break;
  135. }
  136. }
  137. static uint32_t parse_cfg_file(struct ubl_header *ublhdr, char *name)
  138. {
  139. FILE *fd = NULL;
  140. char *line = NULL;
  141. char *token, *saveptr1, *saveptr2;
  142. int lineno = 0;
  143. int i;
  144. char *ptr = (char *)ublhdr;
  145. int fld;
  146. size_t len;
  147. int dcd_len = 0;
  148. int32_t cmd;
  149. int ublhdrlen = sizeof(struct ubl_header);
  150. fd = fopen(name, "r");
  151. if (fd == 0) {
  152. fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
  153. exit(EXIT_FAILURE);
  154. }
  155. /* Fill header with 0xff */
  156. for (i = 0; i < ublhdrlen; i++) {
  157. *ptr = 0xff;
  158. ptr++;
  159. }
  160. /*
  161. * Very simple parsing, line starting with # are comments
  162. * and are dropped
  163. */
  164. while ((getline(&line, &len, fd)) > 0) {
  165. lineno++;
  166. token = strtok_r(line, "\r\n", &saveptr1);
  167. if (token == NULL)
  168. continue;
  169. /* Check inside the single line */
  170. for (fld = CFG_COMMAND, cmd = CMD_INVALID,
  171. line = token; ; line = NULL, fld++) {
  172. token = strtok_r(line, " \t", &saveptr2);
  173. if (token == NULL)
  174. break;
  175. /* Drop all text starting with '#' as comments */
  176. if (token[0] == '#')
  177. break;
  178. parse_cfg_fld(ublhdr, &cmd, token, name,
  179. lineno, fld, &dcd_len);
  180. }
  181. }
  182. fclose(fd);
  183. return dcd_len;
  184. }
  185. static int ublimage_check_image_types(uint8_t type)
  186. {
  187. if (type == IH_TYPE_UBLIMAGE)
  188. return EXIT_SUCCESS;
  189. else
  190. return EXIT_FAILURE;
  191. }
  192. static int ublimage_verify_header(unsigned char *ptr, int image_size,
  193. struct mkimage_params *params)
  194. {
  195. return 0;
  196. }
  197. static void ublimage_print_header(const void *ptr)
  198. {
  199. struct ubl_header *ubl_hdr = (struct ubl_header *) ptr;
  200. print_hdr(ubl_hdr);
  201. }
  202. static void ublimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  203. struct mkimage_params *params)
  204. {
  205. struct ubl_header *ublhdr = (struct ubl_header *)ptr;
  206. /* Parse configuration file */
  207. parse_cfg_file(ublhdr, params->imagename);
  208. }
  209. int ublimage_check_params(struct mkimage_params *params)
  210. {
  211. if (!params)
  212. return CFG_INVALID;
  213. if (!strlen(params->imagename)) {
  214. fprintf(stderr, "Error: %s - Configuration file not"
  215. "specified, it is needed for ublimage generation\n",
  216. params->cmdname);
  217. return CFG_INVALID;
  218. }
  219. /*
  220. * Check parameters:
  221. * XIP is not allowed and verify that incompatible
  222. * parameters are not sent at the same time
  223. * For example, if list is required a data image must not be provided
  224. */
  225. return (params->dflag && (params->fflag || params->lflag)) ||
  226. (params->fflag && (params->dflag || params->lflag)) ||
  227. (params->lflag && (params->dflag || params->fflag)) ||
  228. (params->xflag) || !(strlen(params->imagename));
  229. }
  230. /*
  231. * ublimage parameters
  232. */
  233. static struct image_type_params ublimage_params = {
  234. .name = "Davinci UBL boot support",
  235. .header_size = sizeof(struct ubl_header),
  236. .hdr = (void *)&ublimage_header,
  237. .check_image_type = ublimage_check_image_types,
  238. .verify_header = ublimage_verify_header,
  239. .print_header = ublimage_print_header,
  240. .set_header = ublimage_set_header,
  241. .check_params = ublimage_check_params,
  242. };
  243. void init_ubl_image_type(void)
  244. {
  245. mkimage_register(&ublimage_params);
  246. }