part_iso.c 6.7 KB

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