part_iso.c 7.1 KB

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