ppcboot.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /* BFD back-end for PPCbug boot records.
  2. Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3. Written by Michael Meissner, Cygnus Support, <meissner@cygnus.com>
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* This is a BFD backend which may be used to write PowerPCBug boot objects.
  18. It may only be used for output, not input. The intention is that this may
  19. be used as an output format for objcopy in order to generate raw binary
  20. data.
  21. This is very simple. The only complication is that the real data
  22. will start at some address X, and in some cases we will not want to
  23. include X zeroes just to get to that point. Since the start
  24. address is not meaningful for this object file format, we use it
  25. instead to indicate the number of zeroes to skip at the start of
  26. the file. objcopy cooperates by specially setting the start
  27. address to zero by default. */
  28. #include "sysdep.h"
  29. #include "safe-ctype.h"
  30. #include "bfd.h"
  31. #include "libbfd.h"
  32. /* PPCbug location structure */
  33. typedef struct ppcboot_location
  34. {
  35. bfd_byte ind;
  36. bfd_byte head;
  37. bfd_byte sector;
  38. bfd_byte cylinder;
  39. } ppcboot_location_t;
  40. /* PPCbug partition table layout */
  41. typedef struct ppcboot_partition
  42. {
  43. ppcboot_location_t partition_begin; /* partition begin */
  44. ppcboot_location_t partition_end; /* partition end */
  45. bfd_byte sector_begin[4]; /* 32-bit start RBA (zero-based), little endian */
  46. bfd_byte sector_length[4]; /* 32-bit RBA count (one-based), little endian */
  47. } ppcboot_partition_t;
  48. /* PPCbug boot layout. */
  49. typedef struct ppcboot_hdr
  50. {
  51. bfd_byte pc_compatibility[446]; /* x86 instruction field */
  52. ppcboot_partition_t partition[4]; /* partition information */
  53. bfd_byte signature[2]; /* 0x55 and 0xaa */
  54. bfd_byte entry_offset[4]; /* entry point offset, little endian */
  55. bfd_byte length[4]; /* load image length, little endian */
  56. bfd_byte flags; /* flag field */
  57. bfd_byte os_id; /* OS_ID */
  58. char partition_name[32]; /* partition name */
  59. bfd_byte reserved1[470]; /* reserved */
  60. }
  61. #ifdef __GNUC__
  62. __attribute__ ((packed))
  63. #endif
  64. ppcboot_hdr_t;
  65. /* Signature bytes for last 2 bytes of the 512 byte record */
  66. #define SIGNATURE0 0x55
  67. #define SIGNATURE1 0xaa
  68. /* PowerPC boot type */
  69. #define PPC_IND 0x41
  70. /* Information needed for ppcboot header */
  71. typedef struct ppcboot_data
  72. {
  73. ppcboot_hdr_t header; /* raw header */
  74. asection *sec; /* single section */
  75. } ppcboot_data_t;
  76. /* Any bfd we create by reading a ppcboot file has three symbols:
  77. a start symbol, an end symbol, and an absolute length symbol. */
  78. #define PPCBOOT_SYMS 3
  79. #define ppcboot_set_tdata(abfd, ptr) ((abfd)->tdata.any = (ptr))
  80. #define ppcboot_get_tdata(abfd) ((ppcboot_data_t *) ((abfd)->tdata.any))
  81. /* Create a ppcboot object. Invoked via bfd_set_format. */
  82. static bfd_boolean
  83. ppcboot_mkobject (bfd *abfd)
  84. {
  85. if (!ppcboot_get_tdata (abfd))
  86. {
  87. bfd_size_type amt = sizeof (ppcboot_data_t);
  88. ppcboot_set_tdata (abfd, bfd_zalloc (abfd, amt));
  89. }
  90. return TRUE;
  91. }
  92. /* Set the architecture to PowerPC */
  93. static bfd_boolean
  94. ppcboot_set_arch_mach (bfd *abfd,
  95. enum bfd_architecture arch,
  96. unsigned long machine)
  97. {
  98. if (arch == bfd_arch_unknown)
  99. arch = bfd_arch_powerpc;
  100. else if (arch != bfd_arch_powerpc)
  101. return FALSE;
  102. return bfd_default_set_arch_mach (abfd, arch, machine);
  103. }
  104. /* Any file may be considered to be a ppcboot file, provided the target
  105. was not defaulted. That is, it must be explicitly specified as
  106. being ppcboot. */
  107. static const bfd_target *
  108. ppcboot_object_p (bfd *abfd)
  109. {
  110. struct stat statbuf;
  111. asection *sec;
  112. ppcboot_hdr_t hdr;
  113. size_t i;
  114. ppcboot_data_t *tdata;
  115. flagword flags;
  116. BFD_ASSERT (sizeof (ppcboot_hdr_t) == 1024);
  117. if (abfd->target_defaulted)
  118. {
  119. bfd_set_error (bfd_error_wrong_format);
  120. return NULL;
  121. }
  122. /* Find the file size. */
  123. if (bfd_stat (abfd, &statbuf) < 0)
  124. {
  125. bfd_set_error (bfd_error_system_call);
  126. return NULL;
  127. }
  128. if ((size_t) statbuf.st_size < sizeof (ppcboot_hdr_t))
  129. {
  130. bfd_set_error (bfd_error_wrong_format);
  131. return NULL;
  132. }
  133. if (bfd_bread (&hdr, (bfd_size_type) sizeof (hdr), abfd)
  134. != sizeof (hdr))
  135. {
  136. if (bfd_get_error () != bfd_error_system_call)
  137. bfd_set_error (bfd_error_wrong_format);
  138. return NULL;
  139. }
  140. /* Now do some basic checks. */
  141. for (i = 0; i < sizeof (hdr.pc_compatibility); i++)
  142. if (hdr.pc_compatibility[i])
  143. {
  144. bfd_set_error (bfd_error_wrong_format);
  145. return NULL;
  146. }
  147. if (hdr.signature[0] != SIGNATURE0 || hdr.signature[1] != SIGNATURE1)
  148. {
  149. bfd_set_error (bfd_error_wrong_format);
  150. return NULL;
  151. }
  152. if (hdr.partition[0].partition_end.ind != PPC_IND)
  153. {
  154. bfd_set_error (bfd_error_wrong_format);
  155. return NULL;
  156. }
  157. abfd->symcount = PPCBOOT_SYMS;
  158. /* One data section. */
  159. flags = SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_CODE | SEC_HAS_CONTENTS;
  160. sec = bfd_make_section_with_flags (abfd, ".data", flags);
  161. if (sec == NULL)
  162. return NULL;
  163. sec->vma = 0;
  164. sec->size = statbuf.st_size - sizeof (ppcboot_hdr_t);
  165. sec->filepos = sizeof (ppcboot_hdr_t);
  166. ppcboot_mkobject (abfd);
  167. tdata = ppcboot_get_tdata (abfd);
  168. tdata->sec = sec;
  169. memcpy (&tdata->header, &hdr, sizeof (ppcboot_hdr_t));
  170. ppcboot_set_arch_mach (abfd, bfd_arch_powerpc, 0L);
  171. return abfd->xvec;
  172. }
  173. #define ppcboot_close_and_cleanup _bfd_generic_close_and_cleanup
  174. #define ppcboot_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  175. #define ppcboot_new_section_hook _bfd_generic_new_section_hook
  176. /* Get contents of the only section. */
  177. static bfd_boolean
  178. ppcboot_get_section_contents (bfd *abfd,
  179. asection *section ATTRIBUTE_UNUSED,
  180. void * location,
  181. file_ptr offset,
  182. bfd_size_type count)
  183. {
  184. if (bfd_seek (abfd, offset + (file_ptr) sizeof (ppcboot_hdr_t), SEEK_SET) != 0
  185. || bfd_bread (location, count, abfd) != count)
  186. return FALSE;
  187. return TRUE;
  188. }
  189. /* Return the amount of memory needed to read the symbol table. */
  190. static long
  191. ppcboot_get_symtab_upper_bound (bfd *abfd ATTRIBUTE_UNUSED)
  192. {
  193. return (PPCBOOT_SYMS + 1) * sizeof (asymbol *);
  194. }
  195. /* Create a symbol name based on the bfd's filename. */
  196. static char *
  197. mangle_name (bfd *abfd, char *suffix)
  198. {
  199. bfd_size_type size;
  200. char *buf;
  201. char *p;
  202. size = (strlen (bfd_get_filename (abfd))
  203. + strlen (suffix)
  204. + sizeof "_ppcboot__");
  205. buf = (char *) bfd_alloc (abfd, size);
  206. if (buf == NULL)
  207. return "";
  208. sprintf (buf, "_ppcboot_%s_%s", bfd_get_filename (abfd), suffix);
  209. /* Change any non-alphanumeric characters to underscores. */
  210. for (p = buf; *p; p++)
  211. if (! ISALNUM (*p))
  212. *p = '_';
  213. return buf;
  214. }
  215. /* Return the symbol table. */
  216. static long
  217. ppcboot_canonicalize_symtab (bfd *abfd, asymbol **alocation)
  218. {
  219. asection *sec = ppcboot_get_tdata (abfd)->sec;
  220. asymbol *syms;
  221. unsigned int i;
  222. bfd_size_type amt = PPCBOOT_SYMS * sizeof (asymbol);
  223. syms = (asymbol *) bfd_alloc (abfd, amt);
  224. if (syms == NULL)
  225. return FALSE;
  226. /* Start symbol. */
  227. syms[0].the_bfd = abfd;
  228. syms[0].name = mangle_name (abfd, "start");
  229. syms[0].value = 0;
  230. syms[0].flags = BSF_GLOBAL;
  231. syms[0].section = sec;
  232. syms[0].udata.p = NULL;
  233. /* End symbol. */
  234. syms[1].the_bfd = abfd;
  235. syms[1].name = mangle_name (abfd, "end");
  236. syms[1].value = sec->size;
  237. syms[1].flags = BSF_GLOBAL;
  238. syms[1].section = sec;
  239. syms[1].udata.p = NULL;
  240. /* Size symbol. */
  241. syms[2].the_bfd = abfd;
  242. syms[2].name = mangle_name (abfd, "size");
  243. syms[2].value = sec->size;
  244. syms[2].flags = BSF_GLOBAL;
  245. syms[2].section = bfd_abs_section_ptr;
  246. syms[2].udata.p = NULL;
  247. for (i = 0; i < PPCBOOT_SYMS; i++)
  248. *alocation++ = syms++;
  249. *alocation = NULL;
  250. return PPCBOOT_SYMS;
  251. }
  252. #define ppcboot_make_empty_symbol _bfd_generic_make_empty_symbol
  253. #define ppcboot_print_symbol _bfd_nosymbols_print_symbol
  254. /* Get information about a symbol. */
  255. static void
  256. ppcboot_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED,
  257. asymbol *symbol,
  258. symbol_info *ret)
  259. {
  260. bfd_symbol_info (symbol, ret);
  261. }
  262. #define ppcboot_get_symbol_version_string \
  263. _bfd_nosymbols_get_symbol_version_string
  264. #define ppcboot_bfd_is_target_special_symbol \
  265. ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
  266. #define ppcboot_bfd_is_local_label_name bfd_generic_is_local_label_name
  267. #define ppcboot_get_lineno _bfd_nosymbols_get_lineno
  268. #define ppcboot_find_nearest_line _bfd_nosymbols_find_nearest_line
  269. #define ppcboot_find_line _bfd_nosymbols_find_line
  270. #define ppcboot_find_inliner_info _bfd_nosymbols_find_inliner_info
  271. #define ppcboot_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  272. #define ppcboot_read_minisymbols _bfd_generic_read_minisymbols
  273. #define ppcboot_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  274. /* Write section contents of a ppcboot file. */
  275. static bfd_boolean
  276. ppcboot_set_section_contents (bfd *abfd,
  277. asection *sec,
  278. const void * data,
  279. file_ptr offset,
  280. bfd_size_type size)
  281. {
  282. if (! abfd->output_has_begun)
  283. {
  284. bfd_vma low;
  285. asection *s;
  286. /* The lowest section VMA sets the virtual address of the start
  287. of the file. We use the set the file position of all the
  288. sections. */
  289. low = abfd->sections->vma;
  290. for (s = abfd->sections->next; s != NULL; s = s->next)
  291. if (s->vma < low)
  292. low = s->vma;
  293. for (s = abfd->sections; s != NULL; s = s->next)
  294. s->filepos = s->vma - low;
  295. abfd->output_has_begun = TRUE;
  296. }
  297. return _bfd_generic_set_section_contents (abfd, sec, data, offset, size);
  298. }
  299. static int
  300. ppcboot_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
  301. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  302. {
  303. return sizeof (ppcboot_hdr_t);
  304. }
  305. /* Print out the program headers. */
  306. static bfd_boolean
  307. ppcboot_bfd_print_private_bfd_data (bfd *abfd, void * farg)
  308. {
  309. FILE *f = (FILE *)farg;
  310. ppcboot_data_t *tdata = ppcboot_get_tdata (abfd);
  311. long entry_offset = bfd_getl_signed_32 (tdata->header.entry_offset);
  312. long length = bfd_getl_signed_32 (tdata->header.length);
  313. int i;
  314. fprintf (f, _("\nppcboot header:\n"));
  315. fprintf (f, _("Entry offset = 0x%.8lx (%ld)\n"),
  316. (unsigned long) entry_offset, entry_offset);
  317. fprintf (f, _("Length = 0x%.8lx (%ld)\n"),
  318. (unsigned long) length, length);
  319. if (tdata->header.flags)
  320. fprintf (f, _("Flag field = 0x%.2x\n"), tdata->header.flags);
  321. if (tdata->header.os_id)
  322. fprintf (f, "OS_ID = 0x%.2x\n", tdata->header.os_id);
  323. if (tdata->header.partition_name[0])
  324. fprintf (f, _("Partition name = \"%s\"\n"), tdata->header.partition_name);
  325. for (i = 0; i < 4; i++)
  326. {
  327. long sector_begin = bfd_getl_signed_32 (tdata->header.partition[i].sector_begin);
  328. long sector_length = bfd_getl_signed_32 (tdata->header.partition[i].sector_length);
  329. /* Skip all 0 entries */
  330. if (!tdata->header.partition[i].partition_begin.ind
  331. && !tdata->header.partition[i].partition_begin.head
  332. && !tdata->header.partition[i].partition_begin.sector
  333. && !tdata->header.partition[i].partition_begin.cylinder
  334. && !tdata->header.partition[i].partition_end.ind
  335. && !tdata->header.partition[i].partition_end.head
  336. && !tdata->header.partition[i].partition_end.sector
  337. && !tdata->header.partition[i].partition_end.cylinder
  338. && !sector_begin && !sector_length)
  339. continue;
  340. fprintf (f, _("\nPartition[%d] start = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n"), i,
  341. tdata->header.partition[i].partition_begin.ind,
  342. tdata->header.partition[i].partition_begin.head,
  343. tdata->header.partition[i].partition_begin.sector,
  344. tdata->header.partition[i].partition_begin.cylinder);
  345. fprintf (f, _("Partition[%d] end = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n"), i,
  346. tdata->header.partition[i].partition_end.ind,
  347. tdata->header.partition[i].partition_end.head,
  348. tdata->header.partition[i].partition_end.sector,
  349. tdata->header.partition[i].partition_end.cylinder);
  350. fprintf (f, _("Partition[%d] sector = 0x%.8lx (%ld)\n"),
  351. i, (unsigned long) sector_begin, sector_begin);
  352. fprintf (f, _("Partition[%d] length = 0x%.8lx (%ld)\n"),
  353. i, (unsigned long) sector_length, sector_length);
  354. }
  355. fprintf (f, "\n");
  356. return TRUE;
  357. }
  358. #define ppcboot_bfd_get_relocated_section_contents \
  359. bfd_generic_get_relocated_section_contents
  360. #define ppcboot_bfd_relax_section bfd_generic_relax_section
  361. #define ppcboot_bfd_gc_sections bfd_generic_gc_sections
  362. #define ppcboot_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  363. #define ppcboot_bfd_merge_sections bfd_generic_merge_sections
  364. #define ppcboot_bfd_is_group_section bfd_generic_is_group_section
  365. #define ppcboot_bfd_discard_group bfd_generic_discard_group
  366. #define ppcboot_section_already_linked \
  367. _bfd_generic_section_already_linked
  368. #define ppcboot_bfd_define_common_symbol bfd_generic_define_common_symbol
  369. #define ppcboot_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  370. #define ppcboot_bfd_link_add_symbols _bfd_generic_link_add_symbols
  371. #define ppcboot_bfd_link_just_syms _bfd_generic_link_just_syms
  372. #define ppcboot_bfd_copy_link_hash_symbol_type \
  373. _bfd_generic_copy_link_hash_symbol_type
  374. #define ppcboot_bfd_final_link _bfd_generic_final_link
  375. #define ppcboot_bfd_link_split_section _bfd_generic_link_split_section
  376. #define ppcboot_get_section_contents_in_window \
  377. _bfd_generic_get_section_contents_in_window
  378. #define ppcboot_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
  379. #define ppcboot_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
  380. #define ppcboot_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
  381. #define ppcboot_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
  382. #define ppcboot_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
  383. #define ppcboot_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
  384. #define ppcboot_bfd_print_private_bfd_dat ppcboot_bfd_print_private_bfd_data
  385. const bfd_target powerpc_boot_vec =
  386. {
  387. "ppcboot", /* name */
  388. bfd_target_unknown_flavour, /* flavour */
  389. BFD_ENDIAN_BIG, /* byteorder is big endian for code */
  390. BFD_ENDIAN_LITTLE, /* header_byteorder */
  391. EXEC_P, /* object_flags */
  392. (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
  393. | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
  394. 0, /* symbol_leading_char */
  395. ' ', /* ar_pad_char */
  396. 16, /* ar_max_namelen */
  397. 0, /* match priority. */
  398. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  399. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  400. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
  401. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  402. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  403. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
  404. { /* bfd_check_format */
  405. _bfd_dummy_target,
  406. ppcboot_object_p, /* bfd_check_format */
  407. _bfd_dummy_target,
  408. _bfd_dummy_target,
  409. },
  410. { /* bfd_set_format */
  411. bfd_false,
  412. ppcboot_mkobject,
  413. bfd_false,
  414. bfd_false,
  415. },
  416. { /* bfd_write_contents */
  417. bfd_false,
  418. bfd_true,
  419. bfd_false,
  420. bfd_false,
  421. },
  422. BFD_JUMP_TABLE_GENERIC (ppcboot),
  423. BFD_JUMP_TABLE_COPY (ppcboot),
  424. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  425. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  426. BFD_JUMP_TABLE_SYMBOLS (ppcboot),
  427. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  428. BFD_JUMP_TABLE_WRITE (ppcboot),
  429. BFD_JUMP_TABLE_LINK (ppcboot),
  430. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  431. NULL,
  432. NULL
  433. };