layout.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2005-2008 coresystems GmbH
  5. * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
  6. * Copyright (C) 2011-2013 Stefan Tauner
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <limits.h>
  25. #include "flash.h"
  26. #include "programmer.h"
  27. #define MAX_ROMLAYOUT 32
  28. typedef struct {
  29. chipoff_t start;
  30. chipoff_t end;
  31. unsigned int included;
  32. char name[256];
  33. } romentry_t;
  34. /* rom_entries store the entries specified in a layout file and associated run-time data */
  35. static romentry_t rom_entries[MAX_ROMLAYOUT];
  36. static int num_rom_entries = 0; /* the number of successfully parsed rom_entries */
  37. /* include_args holds the arguments specified at the command line with -i. They must be processed at some point
  38. * so that desired regions are marked as "included" in the rom_entries list. */
  39. static char *include_args[MAX_ROMLAYOUT];
  40. static int num_include_args = 0; /* the number of valid include_args. */
  41. #ifndef __LIBPAYLOAD__
  42. int read_romlayout(const char *name)
  43. {
  44. FILE *romlayout;
  45. char tempstr[256];
  46. int i;
  47. romlayout = fopen(name, "r");
  48. if (!romlayout) {
  49. msg_gerr("ERROR: Could not open ROM layout (%s).\n",
  50. name);
  51. return -1;
  52. }
  53. while (!feof(romlayout)) {
  54. char *tstr1, *tstr2;
  55. if (num_rom_entries >= MAX_ROMLAYOUT) {
  56. msg_gerr("Maximum number of ROM images (%i) in layout "
  57. "file reached.\n", MAX_ROMLAYOUT);
  58. (void)fclose(romlayout);
  59. return 1;
  60. }
  61. if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, rom_entries[num_rom_entries].name))
  62. continue;
  63. #if 0
  64. // fscanf does not like arbitrary comments like that :( later
  65. if (tempstr[0] == '#') {
  66. continue;
  67. }
  68. #endif
  69. tstr1 = strtok(tempstr, ":");
  70. tstr2 = strtok(NULL, ":");
  71. if (!tstr1 || !tstr2) {
  72. msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
  73. (void)fclose(romlayout);
  74. return 1;
  75. }
  76. rom_entries[num_rom_entries].start = strtol(tstr1, (char **)NULL, 16);
  77. rom_entries[num_rom_entries].end = strtol(tstr2, (char **)NULL, 16);
  78. rom_entries[num_rom_entries].included = 0;
  79. num_rom_entries++;
  80. }
  81. for (i = 0; i < num_rom_entries; i++) {
  82. msg_gdbg("romlayout %08x - %08x named %s\n",
  83. rom_entries[i].start,
  84. rom_entries[i].end, rom_entries[i].name);
  85. }
  86. (void)fclose(romlayout);
  87. return 0;
  88. }
  89. #endif
  90. /* returns the index of the entry (or a negative value if it is not found) */
  91. static int find_include_arg(const char *const name)
  92. {
  93. unsigned int i;
  94. for (i = 0; i < num_include_args; i++) {
  95. if (!strcmp(include_args[i], name))
  96. return i;
  97. }
  98. return -1;
  99. }
  100. /* register an include argument (-i) for later processing */
  101. int register_include_arg(char *name)
  102. {
  103. if (num_include_args >= MAX_ROMLAYOUT) {
  104. msg_gerr("Too many regions included (%i).\n", num_include_args);
  105. return 1;
  106. }
  107. if (name == NULL) {
  108. msg_gerr("<NULL> is a bad region name.\n");
  109. return 1;
  110. }
  111. if (find_include_arg(name) != -1) {
  112. msg_gerr("Duplicate region name: \"%s\".\n", name);
  113. return 1;
  114. }
  115. include_args[num_include_args] = name;
  116. num_include_args++;
  117. return 0;
  118. }
  119. /* returns the index of the entry (or a negative value if it is not found) */
  120. static int find_romentry(char *name)
  121. {
  122. int i;
  123. if (num_rom_entries == 0)
  124. return -1;
  125. msg_gspew("Looking for region \"%s\"... ", name);
  126. for (i = 0; i < num_rom_entries; i++) {
  127. if (!strcmp(rom_entries[i].name, name)) {
  128. rom_entries[i].included = 1;
  129. msg_gspew("found.\n");
  130. return i;
  131. }
  132. }
  133. msg_gspew("not found.\n");
  134. return -1;
  135. }
  136. /* process -i arguments
  137. * returns 0 to indicate success, >0 to indicate failure
  138. */
  139. int process_include_args(void)
  140. {
  141. int i;
  142. unsigned int found = 0;
  143. if (num_include_args == 0)
  144. return 0;
  145. /* User has specified an area, but no layout file is loaded. */
  146. if (num_rom_entries == 0) {
  147. msg_gerr("Region requested (with -i \"%s\"), "
  148. "but no layout data is available.\n",
  149. include_args[0]);
  150. return 1;
  151. }
  152. for (i = 0; i < num_include_args; i++) {
  153. if (find_romentry(include_args[i]) < 0) {
  154. msg_gerr("Invalid region specified: \"%s\".\n",
  155. include_args[i]);
  156. return 1;
  157. }
  158. found++;
  159. }
  160. msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "",
  161. include_args[0]);
  162. for (i = 1; i < num_include_args; i++)
  163. msg_ginfo(", \"%s\"", include_args[i]);
  164. msg_ginfo(".\n");
  165. return 0;
  166. }
  167. void layout_cleanup(void)
  168. {
  169. int i;
  170. for (i = 0; i < num_include_args; i++) {
  171. free(include_args[i]);
  172. include_args[i] = NULL;
  173. }
  174. num_include_args = 0;
  175. for (i = 0; i < num_rom_entries; i++) {
  176. rom_entries[i].included = 0;
  177. }
  178. num_rom_entries = 0;
  179. }
  180. romentry_t *get_next_included_romentry(unsigned int start)
  181. {
  182. int i;
  183. unsigned int best_start = UINT_MAX;
  184. romentry_t *best_entry = NULL;
  185. romentry_t *cur;
  186. /* First come, first serve for overlapping regions. */
  187. for (i = 0; i < num_rom_entries; i++) {
  188. cur = &rom_entries[i];
  189. if (!cur->included)
  190. continue;
  191. /* Already past the current entry? */
  192. if (start > cur->end)
  193. continue;
  194. /* Inside the current entry? */
  195. if (start >= cur->start)
  196. return cur;
  197. /* Entry begins after start. */
  198. if (best_start > cur->start) {
  199. best_start = cur->start;
  200. best_entry = cur;
  201. }
  202. }
  203. return best_entry;
  204. }
  205. /* Validate and - if needed - normalize layout entries. */
  206. int normalize_romentries(const struct flashctx *flash)
  207. {
  208. chipsize_t total_size = flash->chip->total_size * 1024;
  209. int ret = 0;
  210. int i;
  211. for (i = 0; i < num_rom_entries; i++) {
  212. if (rom_entries[i].start >= total_size || rom_entries[i].end >= total_size) {
  213. msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's "
  214. "address space.\n", rom_entries[i].name);
  215. if (rom_entries[i].included)
  216. ret = 1;
  217. }
  218. if (rom_entries[i].start > rom_entries[i].end) {
  219. msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
  220. rom_entries[i].name);
  221. ret = 1;
  222. }
  223. }
  224. return ret;
  225. }
  226. static int copy_old_content(struct flashctx *flash, int oldcontents_valid, uint8_t *oldcontents, uint8_t *newcontents, unsigned int start, unsigned int size)
  227. {
  228. if (!oldcontents_valid) {
  229. /* oldcontents is a zero-filled buffer. By reading the current data into oldcontents here, we
  230. * avoid a rewrite of identical regions even if an initial full chip read didn't happen. */
  231. msg_gdbg2("Read a chunk starting at 0x%06x (len=0x%06x).\n", start, size);
  232. int ret = flash->chip->read(flash, oldcontents + start, start, size);
  233. if (ret != 0) {
  234. msg_gerr("Failed to read chunk 0x%06x-0x%06x.\n", start, start + size - 1);
  235. return 1;
  236. }
  237. }
  238. memcpy(newcontents + start, oldcontents + start, size);
  239. return 0;
  240. }
  241. /**
  242. * Modify @newcontents so that it contains the data that should be on the chip eventually. In the case the user
  243. * wants to update only parts of it, copy the chunks to be preserved from @oldcontents to @newcontents. If
  244. * @oldcontents is not valid, we need to fetch the current data from the chip first.
  245. */
  246. int build_new_image(struct flashctx *flash, bool oldcontents_valid, uint8_t *oldcontents, uint8_t *newcontents)
  247. {
  248. unsigned int start = 0;
  249. romentry_t *entry;
  250. unsigned int size = flash->chip->total_size * 1024;
  251. /* If no regions were specified for inclusion, assume
  252. * that the user wants to write the complete new image.
  253. */
  254. if (num_include_args == 0)
  255. return 0;
  256. /* Non-included romentries are ignored.
  257. * The union of all included romentries is used from the new image.
  258. */
  259. while (start < size) {
  260. entry = get_next_included_romentry(start);
  261. /* No more romentries for remaining region? */
  262. if (!entry) {
  263. copy_old_content(flash, oldcontents_valid, oldcontents, newcontents, start,
  264. size - start);
  265. break;
  266. }
  267. /* For non-included region, copy from old content. */
  268. if (entry->start > start)
  269. copy_old_content(flash, oldcontents_valid, oldcontents, newcontents, start,
  270. entry->start - start);
  271. /* Skip to location after current romentry. */
  272. start = entry->end + 1;
  273. /* Catch overflow. */
  274. if (!start)
  275. break;
  276. }
  277. return 0;
  278. }