part_iso.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 get_partition_info_iso_verb(block_dev_desc_t * dev_desc, int part_num, disk_partition_t * info, int verb)
  41. {
  42. int i,offset,entry_num;
  43. unsigned short *chksumbuf;
  44. unsigned short chksum;
  45. unsigned long newblkaddr,blkaddr,lastsect,bootaddr;
  46. iso_boot_rec_t *pbr = (iso_boot_rec_t *)tmpbuf; /* boot record */
  47. iso_pri_rec_t *ppr = (iso_pri_rec_t *)tmpbuf; /* primary desc */
  48. iso_val_entry_t *pve = (iso_val_entry_t *)tmpbuf;
  49. iso_init_def_entry_t *pide;
  50. if (dev_desc->blksz != CD_SECTSIZE)
  51. return -1;
  52. /* the first sector (sector 0x10) must be a primary volume desc */
  53. blkaddr=PVD_OFFSET;
  54. if (dev_desc->block_read(dev_desc, PVD_OFFSET, 1, (ulong *)tmpbuf) != 1)
  55. return -1;
  56. if(ppr->desctype!=0x01) {
  57. if(verb)
  58. printf ("** First descriptor is NOT a primary desc on %d:%d **\n",
  59. dev_desc->dev, part_num);
  60. return (-1);
  61. }
  62. if(strncmp((char *)ppr->stand_ident,"CD001",5)!=0) {
  63. if(verb)
  64. printf ("** Wrong ISO Ident: %s on %d:%d **\n",
  65. ppr->stand_ident,dev_desc->dev, part_num);
  66. return (-1);
  67. }
  68. lastsect= ((ppr->firstsek_LEpathtab1_LE & 0x000000ff)<<24) +
  69. ((ppr->firstsek_LEpathtab1_LE & 0x0000ff00)<< 8) +
  70. ((ppr->firstsek_LEpathtab1_LE & 0x00ff0000)>> 8) +
  71. ((ppr->firstsek_LEpathtab1_LE & 0xff000000)>>24) ;
  72. info->blksz=ppr->secsize_BE; /* assuming same block size for all entries */
  73. PRINTF(" Lastsect:%08lx\n",lastsect);
  74. for(i=blkaddr;i<lastsect;i++) {
  75. PRINTF("Reading block %d\n", i);
  76. if (dev_desc->block_read(dev_desc, i, 1, (ulong *)tmpbuf) != 1)
  77. return -1;
  78. if(ppr->desctype==0x00)
  79. break; /* boot entry found */
  80. if(ppr->desctype==0xff) {
  81. if(verb)
  82. printf ("** No valid boot catalog found on %d:%d **\n",
  83. dev_desc->dev, part_num);
  84. return (-1);
  85. }
  86. }
  87. /* boot entry found */
  88. if(strncmp(pbr->ident_str,"EL TORITO SPECIFICATION",23)!=0) {
  89. if(verb)
  90. printf ("** Wrong El Torito ident: %s on %d:%d **\n",
  91. pbr->ident_str,dev_desc->dev, part_num);
  92. return (-1);
  93. }
  94. bootaddr=le32_to_int(pbr->pointer);
  95. PRINTF(" Boot Entry at: %08lX\n",bootaddr);
  96. if (dev_desc->block_read(dev_desc, bootaddr, 1, (ulong *)tmpbuf) != 1) {
  97. if(verb)
  98. printf ("** Can't read Boot Entry at %lX on %d:%d **\n",
  99. bootaddr,dev_desc->dev, part_num);
  100. return (-1);
  101. }
  102. chksum=0;
  103. chksumbuf = (unsigned short *)tmpbuf;
  104. for(i=0;i<0x10;i++)
  105. chksum+=((chksumbuf[i] &0xff)<<8)+((chksumbuf[i] &0xff00)>>8);
  106. if(chksum!=0) {
  107. if(verb)
  108. printf ("** Checksum Error in booting catalog validation entry on %d:%d **\n",
  109. dev_desc->dev, part_num);
  110. return (-1);
  111. }
  112. if((pve->key[0]!=0x55)||(pve->key[1]!=0xAA)) {
  113. if(verb)
  114. printf ("** Key 0x55 0xAA error on %d:%d **\n",
  115. dev_desc->dev, part_num);
  116. return(-1);
  117. }
  118. #ifdef CHECK_FOR_POWERPC_PLATTFORM
  119. if(pve->platform!=0x01) {
  120. if(verb)
  121. printf ("** No PowerPC platform CD on %d:%d **\n",
  122. dev_desc->dev, part_num);
  123. return(-1);
  124. }
  125. #endif
  126. /* the validation entry seems to be ok, now search the "partition" */
  127. entry_num=0;
  128. offset=0x20;
  129. sprintf ((char *)info->type, "U-Boot");
  130. switch(dev_desc->if_type) {
  131. case IF_TYPE_IDE:
  132. case IF_TYPE_SATA:
  133. case IF_TYPE_ATAPI:
  134. sprintf ((char *)info->name, "hd%c%d",
  135. 'a' + dev_desc->dev, part_num);
  136. break;
  137. case IF_TYPE_SCSI:
  138. sprintf ((char *)info->name, "sd%c%d",
  139. 'a' + dev_desc->dev, part_num);
  140. break;
  141. case IF_TYPE_USB:
  142. sprintf ((char *)info->name, "usbd%c%d",
  143. 'a' + dev_desc->dev, part_num);
  144. break;
  145. case IF_TYPE_DOC:
  146. sprintf ((char *)info->name, "docd%c%d",
  147. 'a' + dev_desc->dev, part_num);
  148. break;
  149. default:
  150. sprintf ((char *)info->name, "xx%c%d",
  151. 'a' + dev_desc->dev, part_num);
  152. break;
  153. }
  154. /* the bootcatalog (including validation Entry) is limited to 2048Bytes
  155. * (63 boot entries + validation entry) */
  156. while(offset<2048) {
  157. pide=(iso_init_def_entry_t *)&tmpbuf[offset];
  158. if ((pide->boot_ind==0x88) ||
  159. (pide->boot_ind==0x00)) { /* Header Id for default Sections Entries */
  160. if(entry_num==part_num) { /* part found */
  161. goto found;
  162. }
  163. entry_num++; /* count partitions Entries (boot and non bootables */
  164. offset+=0x20;
  165. continue;
  166. }
  167. if ((pide->boot_ind==0x90) || /* Section Header Entry */
  168. (pide->boot_ind==0x91) || /* Section Header Entry (last) */
  169. (pide->boot_ind==0x44)) { /* Extension Indicator */
  170. offset+=0x20; /* skip unused entries */
  171. }
  172. else {
  173. if(verb)
  174. printf ("** Partition %d not found on device %d **\n",
  175. part_num,dev_desc->dev);
  176. return(-1);
  177. }
  178. }
  179. /* if we reach this point entire sector has been
  180. * searched w/o succsess */
  181. if(verb)
  182. printf ("** Partition %d not found on device %d **\n",
  183. part_num,dev_desc->dev);
  184. return(-1);
  185. found:
  186. if(pide->boot_ind!=0x88) {
  187. if(verb)
  188. printf ("** Partition %d is not bootable on device %d **\n",
  189. part_num,dev_desc->dev);
  190. return (-1);
  191. }
  192. switch(pide->boot_media) {
  193. case 0x00: /* no emulation */
  194. info->size=le16_to_int(pide->sec_cnt)>>2;
  195. break;
  196. case 0x01: info->size=2400>>2; break; /* 1.2MByte Floppy */
  197. case 0x02: info->size=2880>>2; break; /* 1.44MByte Floppy */
  198. case 0x03: info->size=5760>>2; break; /* 2.88MByte Floppy */
  199. case 0x04: info->size=2880>>2; break; /* dummy (HD Emulation) */
  200. default: info->size=0; break;
  201. }
  202. newblkaddr=le32_to_int(pide->rel_block_addr);
  203. info->start=newblkaddr;
  204. PRINTF(" part %d found @ %lx size %lx\n",part_num,newblkaddr,info->size);
  205. return 0;
  206. }
  207. int get_partition_info_iso(block_dev_desc_t * dev_desc, int part_num, disk_partition_t * info)
  208. {
  209. return(get_partition_info_iso_verb(dev_desc, part_num, info, 1));
  210. }
  211. void print_part_iso(block_dev_desc_t * dev_desc)
  212. {
  213. disk_partition_t info;
  214. int i;
  215. if(get_partition_info_iso_verb(dev_desc,0,&info,0)==-1) {
  216. printf("** No boot partition found on device %d **\n",dev_desc->dev);
  217. return;
  218. }
  219. printf("Part Start Sect x Size Type\n");
  220. i=0;
  221. do {
  222. printf(" %2d " LBAFU " " LBAFU " %6ld %.32s\n",
  223. i, info.start, info.size, info.blksz, info.type);
  224. i++;
  225. } while (get_partition_info_iso_verb(dev_desc,i,&info,0)!=-1);
  226. }
  227. int test_part_iso (block_dev_desc_t *dev_desc)
  228. {
  229. disk_partition_t info;
  230. return(get_partition_info_iso_verb(dev_desc,0,&info,0));
  231. }
  232. #endif