part_iso.c 6.7 KB

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