ppcboot.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /* BFD back-end for PPCbug boot records.
  2. Copyright (C) 1996-2022 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 bool
  83. ppcboot_mkobject (bfd *abfd)
  84. {
  85. if (!ppcboot_get_tdata (abfd))
  86. {
  87. size_t 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 bool
  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 bfd_cleanup
  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 _bfd_no_cleanup;
  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 bool
  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. size_t 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 _bfd_bool_bfd_asymbol_false
  265. #define ppcboot_bfd_is_local_label_name bfd_generic_is_local_label_name
  266. #define ppcboot_get_lineno _bfd_nosymbols_get_lineno
  267. #define ppcboot_find_nearest_line _bfd_nosymbols_find_nearest_line
  268. #define ppcboot_find_line _bfd_nosymbols_find_line
  269. #define ppcboot_find_inliner_info _bfd_nosymbols_find_inliner_info
  270. #define ppcboot_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  271. #define ppcboot_read_minisymbols _bfd_generic_read_minisymbols
  272. #define ppcboot_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  273. /* Write section contents of a ppcboot file. */
  274. static bool
  275. ppcboot_set_section_contents (bfd *abfd,
  276. asection *sec,
  277. const void * data,
  278. file_ptr offset,
  279. bfd_size_type size)
  280. {
  281. if (! abfd->output_has_begun)
  282. {
  283. bfd_vma low;
  284. asection *s;
  285. /* The lowest section VMA sets the virtual address of the start
  286. of the file. We use the set the file position of all the
  287. sections. */
  288. low = abfd->sections->vma;
  289. for (s = abfd->sections->next; s != NULL; s = s->next)
  290. if (s->vma < low)
  291. low = s->vma;
  292. for (s = abfd->sections; s != NULL; s = s->next)
  293. s->filepos = s->vma - low;
  294. abfd->output_has_begun = true;
  295. }
  296. return _bfd_generic_set_section_contents (abfd, sec, data, offset, size);
  297. }
  298. static int
  299. ppcboot_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
  300. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  301. {
  302. return sizeof (ppcboot_hdr_t);
  303. }
  304. /* Print out the program headers. */
  305. static bool
  306. ppcboot_bfd_print_private_bfd_data (bfd *abfd, void * farg)
  307. {
  308. FILE *f = (FILE *)farg;
  309. ppcboot_data_t *tdata = ppcboot_get_tdata (abfd);
  310. long entry_offset = bfd_getl_signed_32 (tdata->header.entry_offset);
  311. long length = bfd_getl_signed_32 (tdata->header.length);
  312. int i;
  313. fprintf (f, _("\nppcboot header:\n"));
  314. fprintf (f, _("Entry offset = 0x%.8lx (%ld)\n"),
  315. (unsigned long) entry_offset, entry_offset);
  316. fprintf (f, _("Length = 0x%.8lx (%ld)\n"),
  317. (unsigned long) length, length);
  318. if (tdata->header.flags)
  319. fprintf (f, _("Flag field = 0x%.2x\n"), tdata->header.flags);
  320. if (tdata->header.os_id)
  321. fprintf (f, "OS_ID = 0x%.2x\n", tdata->header.os_id);
  322. if (tdata->header.partition_name[0])
  323. fprintf (f, _("Partition name = \"%s\"\n"), tdata->header.partition_name);
  324. for (i = 0; i < 4; i++)
  325. {
  326. long sector_begin = bfd_getl_signed_32 (tdata->header.partition[i].sector_begin);
  327. long sector_length = bfd_getl_signed_32 (tdata->header.partition[i].sector_length);
  328. /* Skip all 0 entries */
  329. if (!tdata->header.partition[i].partition_begin.ind
  330. && !tdata->header.partition[i].partition_begin.head
  331. && !tdata->header.partition[i].partition_begin.sector
  332. && !tdata->header.partition[i].partition_begin.cylinder
  333. && !tdata->header.partition[i].partition_end.ind
  334. && !tdata->header.partition[i].partition_end.head
  335. && !tdata->header.partition[i].partition_end.sector
  336. && !tdata->header.partition[i].partition_end.cylinder
  337. && !sector_begin && !sector_length)
  338. continue;
  339. /* xgettext:c-format */
  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. /* xgettext:c-format */
  346. fprintf (f, _("Partition[%d] end = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n"), i,
  347. tdata->header.partition[i].partition_end.ind,
  348. tdata->header.partition[i].partition_end.head,
  349. tdata->header.partition[i].partition_end.sector,
  350. tdata->header.partition[i].partition_end.cylinder);
  351. /* xgettext:c-format */
  352. fprintf (f, _("Partition[%d] sector = 0x%.8lx (%ld)\n"),
  353. i, (unsigned long) sector_begin, sector_begin);
  354. /* xgettext:c-format */
  355. fprintf (f, _("Partition[%d] length = 0x%.8lx (%ld)\n"),
  356. i, (unsigned long) sector_length, sector_length);
  357. }
  358. fprintf (f, "\n");
  359. return true;
  360. }
  361. #define ppcboot_bfd_get_relocated_section_contents \
  362. bfd_generic_get_relocated_section_contents
  363. #define ppcboot_bfd_relax_section bfd_generic_relax_section
  364. #define ppcboot_bfd_gc_sections bfd_generic_gc_sections
  365. #define ppcboot_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  366. #define ppcboot_bfd_merge_sections bfd_generic_merge_sections
  367. #define ppcboot_bfd_is_group_section bfd_generic_is_group_section
  368. #define ppcboot_bfd_group_name bfd_generic_group_name
  369. #define ppcboot_bfd_discard_group bfd_generic_discard_group
  370. #define ppcboot_section_already_linked \
  371. _bfd_generic_section_already_linked
  372. #define ppcboot_bfd_define_common_symbol bfd_generic_define_common_symbol
  373. #define ppcboot_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
  374. #define ppcboot_bfd_define_start_stop bfd_generic_define_start_stop
  375. #define ppcboot_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  376. #define ppcboot_bfd_link_add_symbols _bfd_generic_link_add_symbols
  377. #define ppcboot_bfd_link_just_syms _bfd_generic_link_just_syms
  378. #define ppcboot_bfd_copy_link_hash_symbol_type \
  379. _bfd_generic_copy_link_hash_symbol_type
  380. #define ppcboot_bfd_final_link _bfd_generic_final_link
  381. #define ppcboot_bfd_link_split_section _bfd_generic_link_split_section
  382. #define ppcboot_get_section_contents_in_window \
  383. _bfd_generic_get_section_contents_in_window
  384. #define ppcboot_bfd_link_check_relocs _bfd_generic_link_check_relocs
  385. #define ppcboot_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
  386. #define ppcboot_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
  387. #define ppcboot_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
  388. #define ppcboot_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
  389. #define ppcboot_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
  390. #define ppcboot_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
  391. #define ppcboot_bfd_print_private_bfd_dat ppcboot_bfd_print_private_bfd_data
  392. const bfd_target powerpc_boot_vec =
  393. {
  394. "ppcboot", /* name */
  395. bfd_target_unknown_flavour, /* flavour */
  396. BFD_ENDIAN_BIG, /* byteorder is big endian for code */
  397. BFD_ENDIAN_LITTLE, /* header_byteorder */
  398. EXEC_P, /* object_flags */
  399. (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
  400. | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
  401. 0, /* symbol_leading_char */
  402. ' ', /* ar_pad_char */
  403. 16, /* ar_max_namelen */
  404. 0, /* match priority. */
  405. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  406. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  407. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  408. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
  409. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  410. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  411. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
  412. { /* bfd_check_format */
  413. _bfd_dummy_target,
  414. ppcboot_object_p, /* bfd_check_format */
  415. _bfd_dummy_target,
  416. _bfd_dummy_target,
  417. },
  418. { /* bfd_set_format */
  419. _bfd_bool_bfd_false_error,
  420. ppcboot_mkobject,
  421. _bfd_bool_bfd_false_error,
  422. _bfd_bool_bfd_false_error,
  423. },
  424. { /* bfd_write_contents */
  425. _bfd_bool_bfd_false_error,
  426. _bfd_bool_bfd_true,
  427. _bfd_bool_bfd_false_error,
  428. _bfd_bool_bfd_false_error,
  429. },
  430. BFD_JUMP_TABLE_GENERIC (ppcboot),
  431. BFD_JUMP_TABLE_COPY (ppcboot),
  432. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  433. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  434. BFD_JUMP_TABLE_SYMBOLS (ppcboot),
  435. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  436. BFD_JUMP_TABLE_WRITE (ppcboot),
  437. BFD_JUMP_TABLE_LINK (ppcboot),
  438. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  439. NULL,
  440. NULL
  441. };