part_iso.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include "part_iso.h"
  10. #ifdef HAVE_BLOCK_DEVICE
  11. /* #define ISO_PART_DEBUG */
  12. #ifdef ISO_PART_DEBUG
  13. #define PRINTF(fmt,args...) printf (fmt ,##args)
  14. #else
  15. #define PRINTF(fmt,args...)
  16. #endif
  17. /* enable this if CDs are written with the PowerPC Platform ID */
  18. #undef CHECK_FOR_POWERPC_PLATTFORM
  19. #define CD_SECTSIZE 2048
  20. static unsigned char tmpbuf[CD_SECTSIZE];
  21. /* Convert char[4] in little endian format to the host format integer
  22. */
  23. static inline unsigned long le32_to_int(unsigned char *le32)
  24. {
  25. return ((le32[3] << 24) +
  26. (le32[2] << 16) +
  27. (le32[1] << 8) +
  28. le32[0]
  29. );
  30. }
  31. /* Convert char[2] in little endian format to the host format integer
  32. */
  33. static inline unsigned short le16_to_int(unsigned char *le16)
  34. {
  35. return ((le16[1] << 8) +
  36. le16[0]
  37. );
  38. }
  39. /* only boot records will be listed as valid partitions */
  40. int part_get_info_iso_verb(struct blk_desc *dev_desc, int part_num,
  41. disk_partition_t *info, int verb)
  42. {
  43. int i,offset,entry_num;
  44. unsigned short *chksumbuf;
  45. unsigned short chksum;
  46. unsigned long newblkaddr,blkaddr,lastsect,bootaddr;
  47. iso_boot_rec_t *pbr = (iso_boot_rec_t *)tmpbuf; /* boot record */
  48. iso_pri_rec_t *ppr = (iso_pri_rec_t *)tmpbuf; /* primary desc */
  49. iso_val_entry_t *pve = (iso_val_entry_t *)tmpbuf;
  50. iso_init_def_entry_t *pide;
  51. if (dev_desc->blksz != CD_SECTSIZE)
  52. return -1;
  53. /* the first sector (sector 0x10) must be a primary volume desc */
  54. blkaddr=PVD_OFFSET;
  55. if (blk_dread(dev_desc, PVD_OFFSET, 1, (ulong *)tmpbuf) != 1)
  56. return -1;
  57. if(ppr->desctype!=0x01) {
  58. if(verb)
  59. printf ("** First descriptor is NOT a primary desc on %d:%d **\n",
  60. dev_desc->devnum, part_num);
  61. return (-1);
  62. }
  63. if(strncmp((char *)ppr->stand_ident,"CD001",5)!=0) {
  64. if(verb)
  65. printf ("** Wrong ISO Ident: %s on %d:%d **\n",
  66. ppr->stand_ident, dev_desc->devnum, part_num);
  67. return (-1);
  68. }
  69. lastsect= ((ppr->firstsek_LEpathtab1_LE & 0x000000ff)<<24) +
  70. ((ppr->firstsek_LEpathtab1_LE & 0x0000ff00)<< 8) +
  71. ((ppr->firstsek_LEpathtab1_LE & 0x00ff0000)>> 8) +
  72. ((ppr->firstsek_LEpathtab1_LE & 0xff000000)>>24) ;
  73. info->blksz=ppr->secsize_BE; /* assuming same block size for all entries */
  74. PRINTF(" Lastsect:%08lx\n",lastsect);
  75. for(i=blkaddr;i<lastsect;i++) {
  76. PRINTF("Reading block %d\n", i);
  77. if (blk_dread(dev_desc, i, 1, (ulong *)tmpbuf) != 1)
  78. return -1;
  79. if(ppr->desctype==0x00)
  80. break; /* boot entry found */
  81. if(ppr->desctype==0xff) {
  82. if(verb)
  83. printf ("** No valid boot catalog found on %d:%d **\n",
  84. dev_desc->devnum, part_num);
  85. return (-1);
  86. }
  87. }
  88. /* boot entry found */
  89. if(strncmp(pbr->ident_str,"EL TORITO SPECIFICATION",23)!=0) {
  90. if(verb)
  91. printf ("** Wrong El Torito ident: %s on %d:%d **\n",
  92. pbr->ident_str, dev_desc->devnum, part_num);
  93. return (-1);
  94. }
  95. bootaddr=le32_to_int(pbr->pointer);
  96. PRINTF(" Boot Entry at: %08lX\n",bootaddr);
  97. if (blk_dread(dev_desc, bootaddr, 1, (ulong *)tmpbuf) != 1) {
  98. if(verb)
  99. printf ("** Can't read Boot Entry at %lX on %d:%d **\n",
  100. bootaddr, dev_desc->devnum, part_num);
  101. return (-1);
  102. }
  103. chksum=0;
  104. chksumbuf = (unsigned short *)tmpbuf;
  105. for(i=0;i<0x10;i++)
  106. chksum+=((chksumbuf[i] &0xff)<<8)+((chksumbuf[i] &0xff00)>>8);
  107. if(chksum!=0) {
  108. if(verb)
  109. printf("** Checksum Error in booting catalog validation entry on %d:%d **\n",
  110. dev_desc->devnum, part_num);
  111. return (-1);
  112. }
  113. if((pve->key[0]!=0x55)||(pve->key[1]!=0xAA)) {
  114. if(verb)
  115. printf ("** Key 0x55 0xAA error on %d:%d **\n",
  116. dev_desc->devnum, part_num);
  117. return(-1);
  118. }
  119. #ifdef CHECK_FOR_POWERPC_PLATTFORM
  120. if(pve->platform!=0x01) {
  121. if(verb)
  122. printf ("** No PowerPC platform CD on %d:%d **\n",
  123. dev_desc->devnum, part_num);
  124. return(-1);
  125. }
  126. #endif
  127. /* the validation entry seems to be ok, now search the "partition" */
  128. entry_num=0;
  129. offset=0x20;
  130. strcpy((char *)info->type, "U-Boot");
  131. switch(dev_desc->if_type) {
  132. case IF_TYPE_IDE:
  133. case IF_TYPE_SATA:
  134. case IF_TYPE_ATAPI:
  135. sprintf ((char *)info->name, "hd%c%d",
  136. 'a' + dev_desc->devnum, part_num);
  137. break;
  138. case IF_TYPE_SCSI:
  139. sprintf ((char *)info->name, "sd%c%d",
  140. 'a' + dev_desc->devnum, part_num);
  141. break;
  142. case IF_TYPE_USB:
  143. sprintf ((char *)info->name, "usbd%c%d",
  144. 'a' + dev_desc->devnum, part_num);
  145. break;
  146. case IF_TYPE_DOC:
  147. sprintf ((char *)info->name, "docd%c%d",
  148. 'a' + dev_desc->devnum, part_num);
  149. break;
  150. default:
  151. sprintf ((char *)info->name, "xx%c%d",
  152. 'a' + dev_desc->devnum, part_num);
  153. break;
  154. }
  155. /* the bootcatalog (including validation Entry) is limited to 2048Bytes
  156. * (63 boot entries + validation entry) */
  157. while(offset<2048) {
  158. pide=(iso_init_def_entry_t *)&tmpbuf[offset];
  159. if ((pide->boot_ind==0x88) ||
  160. (pide->boot_ind==0x00)) { /* Header Id for default Sections Entries */
  161. if(entry_num==part_num) { /* part found */
  162. goto found;
  163. }
  164. entry_num++; /* count partitions Entries (boot and non bootables */
  165. offset+=0x20;
  166. continue;
  167. }
  168. if ((pide->boot_ind==0x90) || /* Section Header Entry */
  169. (pide->boot_ind==0x91) || /* Section Header Entry (last) */
  170. (pide->boot_ind==0x44)) { /* Extension Indicator */
  171. offset+=0x20; /* skip unused entries */
  172. }
  173. else {
  174. if(verb)
  175. printf ("** Partition %d not found on device %d **\n",
  176. part_num, dev_desc->devnum);
  177. return(-1);
  178. }
  179. }
  180. /* if we reach this point entire sector has been
  181. * searched w/o succsess */
  182. if(verb)
  183. printf ("** Partition %d not found on device %d **\n",
  184. part_num, dev_desc->devnum);
  185. return(-1);
  186. found:
  187. if(pide->boot_ind!=0x88) {
  188. if(verb)
  189. printf("** Partition %d is not bootable on device %d **\n",
  190. part_num, dev_desc->devnum);
  191. return (-1);
  192. }
  193. switch(pide->boot_media) {
  194. case 0x00: /* no emulation */
  195. info->size=le16_to_int(pide->sec_cnt)>>2;
  196. break;
  197. case 0x01: info->size=2400>>2; break; /* 1.2MByte Floppy */
  198. case 0x02: info->size=2880>>2; break; /* 1.44MByte Floppy */
  199. case 0x03: info->size=5760>>2; break; /* 2.88MByte Floppy */
  200. case 0x04: info->size=2880>>2; break; /* dummy (HD Emulation) */
  201. default: info->size=0; break;
  202. }
  203. newblkaddr=le32_to_int(pide->rel_block_addr);
  204. info->start=newblkaddr;
  205. PRINTF(" part %d found @ %lx size %lx\n",part_num,newblkaddr,info->size);
  206. return 0;
  207. }
  208. static int part_get_info_iso(struct blk_desc *dev_desc, int part_num,
  209. disk_partition_t *info)
  210. {
  211. return part_get_info_iso_verb(dev_desc, part_num, info, 1);
  212. }
  213. static void part_print_iso(struct blk_desc *dev_desc)
  214. {
  215. disk_partition_t info;
  216. int i;
  217. if (part_get_info_iso_verb(dev_desc, 0, &info, 0) == -1) {
  218. printf("** No boot partition found on device %d **\n",
  219. dev_desc->devnum);
  220. return;
  221. }
  222. printf("Part Start Sect x Size Type\n");
  223. i=0;
  224. do {
  225. printf(" %2d " LBAFU " " LBAFU " %6ld %.32s\n",
  226. i, info.start, info.size, info.blksz, info.type);
  227. i++;
  228. } while (part_get_info_iso_verb(dev_desc, i, &info, 0) != -1);
  229. }
  230. static int part_test_iso(struct blk_desc *dev_desc)
  231. {
  232. disk_partition_t info;
  233. return part_get_info_iso_verb(dev_desc, 0, &info, 0);
  234. }
  235. U_BOOT_PART_TYPE(iso) = {
  236. .name = "ISO",
  237. .part_type = PART_TYPE_ISO,
  238. .get_info = part_get_info_iso,
  239. .print = part_print_iso,
  240. .test = part_test_iso,
  241. };
  242. #endif