ublimage.c 6.1 KB

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