blob: 1061f341d3532dc2062b5a4d3568253849071a88 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkcc1c8a12002-11-02 22:58:18 +00002/*
3 * (C) Copyright 2001
4 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch.
wdenkcc1c8a12002-11-02 22:58:18 +00005 */
6
7#include <common.h>
Simon Glass655306c2020-05-10 11:39:58 -06008#include <blk.h>
wdenkcc1c8a12002-11-02 22:58:18 +00009#include <command.h>
Simon Glass655306c2020-05-10 11:39:58 -060010#include <part.h>
Simon Glass274e0b02020-05-10 11:39:56 -060011#include <asm/cache.h>
Simon Glassa8c64972016-03-16 07:45:34 -060012#include <asm/unaligned.h>
wdenkcc1c8a12002-11-02 22:58:18 +000013#include "part_iso.h"
14
Adam Fordb10ba902018-02-06 12:43:56 -060015#ifdef CONFIG_HAVE_BLOCK_DEVICE
wdenkcc1c8a12002-11-02 22:58:18 +000016
wdenkacd05362005-02-24 23:23:29 +000017/* #define ISO_PART_DEBUG */
wdenkcc1c8a12002-11-02 22:58:18 +000018
19#ifdef ISO_PART_DEBUG
20#define PRINTF(fmt,args...) printf (fmt ,##args)
21#else
22#define PRINTF(fmt,args...)
23#endif
24
25/* enable this if CDs are written with the PowerPC Platform ID */
26#undef CHECK_FOR_POWERPC_PLATTFORM
27#define CD_SECTSIZE 2048
28
Stefan Agnerdcab2842017-08-23 09:46:17 -070029static unsigned char tmpbuf[CD_SECTSIZE] __aligned(ARCH_DMA_MINALIGN);
wdenkcc1c8a12002-11-02 22:58:18 +000030
Alexander Graf90a199e2016-04-11 16:16:16 +020031unsigned long iso_dread(struct blk_desc *block_dev, lbaint_t start,
32 lbaint_t blkcnt, void *buffer)
33{
34 unsigned long ret;
35
36 if (block_dev->blksz == 512) {
37 /* Convert from 2048 to 512 sector size */
38 start *= 4;
39 blkcnt *= 4;
40 }
41
42 ret = blk_dread(block_dev, start, blkcnt, buffer);
43
44 if (block_dev->blksz == 512)
45 ret /= 4;
46
47 return ret;
48}
49
wdenkcc1c8a12002-11-02 22:58:18 +000050/* only boot records will be listed as valid partitions */
Simon Glassb89a8442016-02-29 15:25:48 -070051int part_get_info_iso_verb(struct blk_desc *dev_desc, int part_num,
Simon Glassc1c4a8f2020-05-10 11:39:57 -060052 struct disk_partition *info, int verb)
wdenkcc1c8a12002-11-02 22:58:18 +000053{
54 int i,offset,entry_num;
55 unsigned short *chksumbuf;
56 unsigned short chksum;
57 unsigned long newblkaddr,blkaddr,lastsect,bootaddr;
58 iso_boot_rec_t *pbr = (iso_boot_rec_t *)tmpbuf; /* boot record */
59 iso_pri_rec_t *ppr = (iso_pri_rec_t *)tmpbuf; /* primary desc */
60 iso_val_entry_t *pve = (iso_val_entry_t *)tmpbuf;
61 iso_init_def_entry_t *pide;
62
Alexander Graf90a199e2016-04-11 16:16:16 +020063 if ((dev_desc->blksz != CD_SECTSIZE) && (dev_desc->blksz != 512))
Egbert Eich9b2ecea2013-04-09 21:11:54 +000064 return -1;
65
wdenkcc1c8a12002-11-02 22:58:18 +000066 /* the first sector (sector 0x10) must be a primary volume desc */
67 blkaddr=PVD_OFFSET;
Alexander Graf90a199e2016-04-11 16:16:16 +020068 if (iso_dread(dev_desc, PVD_OFFSET, 1, (ulong *)tmpbuf) != 1)
Stephen Warrene73f2962015-12-07 11:38:48 -070069 return -1;
wdenkcc1c8a12002-11-02 22:58:18 +000070 if(ppr->desctype!=0x01) {
71 if(verb)
72 printf ("** First descriptor is NOT a primary desc on %d:%d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -070073 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +000074 return (-1);
75 }
Wolfgang Denk7fb52662005-10-13 16:45:02 +020076 if(strncmp((char *)ppr->stand_ident,"CD001",5)!=0) {
wdenkcc1c8a12002-11-02 22:58:18 +000077 if(verb)
78 printf ("** Wrong ISO Ident: %s on %d:%d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -070079 ppr->stand_ident, dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +000080 return (-1);
81 }
Alexander Graf168c1e82016-04-11 16:16:14 +020082 lastsect = le32_to_cpu(ppr->firstsek_LEpathtab1_LE);
83 /* assuming same block size for all entries */
84 info->blksz = be16_to_cpu(ppr->secsize_BE);
wdenkcc1c8a12002-11-02 22:58:18 +000085 PRINTF(" Lastsect:%08lx\n",lastsect);
86 for(i=blkaddr;i<lastsect;i++) {
wdenk452cfd62002-11-19 11:04:11 +000087 PRINTF("Reading block %d\n", i);
Alexander Graf90a199e2016-04-11 16:16:16 +020088 if (iso_dread(dev_desc, i, 1, (ulong *)tmpbuf) != 1)
Stephen Warrene73f2962015-12-07 11:38:48 -070089 return -1;
wdenkcc1c8a12002-11-02 22:58:18 +000090 if(ppr->desctype==0x00)
91 break; /* boot entry found */
92 if(ppr->desctype==0xff) {
93 if(verb)
94 printf ("** No valid boot catalog found on %d:%d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -070095 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +000096 return (-1);
97 }
98 }
Wolfgang Denka1be4762008-05-20 16:00:29 +020099 /* boot entry found */
wdenkcc1c8a12002-11-02 22:58:18 +0000100 if(strncmp(pbr->ident_str,"EL TORITO SPECIFICATION",23)!=0) {
101 if(verb)
102 printf ("** Wrong El Torito ident: %s on %d:%d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700103 pbr->ident_str, dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000104 return (-1);
105 }
Simon Glassa8c64972016-03-16 07:45:34 -0600106 bootaddr = get_unaligned_le32(pbr->pointer);
wdenkcc1c8a12002-11-02 22:58:18 +0000107 PRINTF(" Boot Entry at: %08lX\n",bootaddr);
Alexander Graf90a199e2016-04-11 16:16:16 +0200108 if (iso_dread(dev_desc, bootaddr, 1, (ulong *)tmpbuf) != 1) {
wdenkcc1c8a12002-11-02 22:58:18 +0000109 if(verb)
110 printf ("** Can't read Boot Entry at %lX on %d:%d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700111 bootaddr, dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000112 return (-1);
113 }
114 chksum=0;
115 chksumbuf = (unsigned short *)tmpbuf;
116 for(i=0;i<0x10;i++)
Alexander Graf168c1e82016-04-11 16:16:14 +0200117 chksum += le16_to_cpu(chksumbuf[i]);
wdenkcc1c8a12002-11-02 22:58:18 +0000118 if(chksum!=0) {
119 if(verb)
Simon Glass2f26fff2016-02-29 15:25:51 -0700120 printf("** Checksum Error in booting catalog validation entry on %d:%d **\n",
121 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000122 return (-1);
123 }
124 if((pve->key[0]!=0x55)||(pve->key[1]!=0xAA)) {
125 if(verb)
126 printf ("** Key 0x55 0xAA error on %d:%d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700127 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000128 return(-1);
129 }
130#ifdef CHECK_FOR_POWERPC_PLATTFORM
131 if(pve->platform!=0x01) {
132 if(verb)
133 printf ("** No PowerPC platform CD on %d:%d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700134 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000135 return(-1);
136 }
137#endif
138 /* the validation entry seems to be ok, now search the "partition" */
Alexander Grafb0c13262016-04-11 16:16:15 +0200139 entry_num=1;
wdenkcc1c8a12002-11-02 22:58:18 +0000140 offset=0x20;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000141 strcpy((char *)info->type, "U-Boot");
Petr Kulhavy2e790d52016-09-09 10:27:17 +0200142 part_set_generic_name(dev_desc, part_num, (char *)info->name);
wdenkcc1c8a12002-11-02 22:58:18 +0000143 /* the bootcatalog (including validation Entry) is limited to 2048Bytes
144 * (63 boot entries + validation entry) */
145 while(offset<2048) {
146 pide=(iso_init_def_entry_t *)&tmpbuf[offset];
147 if ((pide->boot_ind==0x88) ||
148 (pide->boot_ind==0x00)) { /* Header Id for default Sections Entries */
149 if(entry_num==part_num) { /* part found */
150 goto found;
151 }
152 entry_num++; /* count partitions Entries (boot and non bootables */
153 offset+=0x20;
154 continue;
155 }
156 if ((pide->boot_ind==0x90) || /* Section Header Entry */
157 (pide->boot_ind==0x91) || /* Section Header Entry (last) */
158 (pide->boot_ind==0x44)) { /* Extension Indicator */
159 offset+=0x20; /* skip unused entries */
160 }
161 else {
162 if(verb)
163 printf ("** Partition %d not found on device %d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700164 part_num, dev_desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000165 return(-1);
166 }
167 }
168 /* if we reach this point entire sector has been
169 * searched w/o succsess */
170 if(verb)
171 printf ("** Partition %d not found on device %d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700172 part_num, dev_desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000173 return(-1);
174found:
175 if(pide->boot_ind!=0x88) {
176 if(verb)
Simon Glass2f26fff2016-02-29 15:25:51 -0700177 printf("** Partition %d is not bootable on device %d **\n",
178 part_num, dev_desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000179 return (-1);
180 }
181 switch(pide->boot_media) {
182 case 0x00: /* no emulation */
Simon Glassa8c64972016-03-16 07:45:34 -0600183 info->size = get_unaligned_le16(pide->sec_cnt)>>2;
wdenkcc1c8a12002-11-02 22:58:18 +0000184 break;
185 case 0x01: info->size=2400>>2; break; /* 1.2MByte Floppy */
186 case 0x02: info->size=2880>>2; break; /* 1.44MByte Floppy */
187 case 0x03: info->size=5760>>2; break; /* 2.88MByte Floppy */
188 case 0x04: info->size=2880>>2; break; /* dummy (HD Emulation) */
189 default: info->size=0; break;
190 }
Simon Glassa8c64972016-03-16 07:45:34 -0600191 newblkaddr = get_unaligned_le32(pide->rel_block_addr);
wdenkcc1c8a12002-11-02 22:58:18 +0000192 info->start=newblkaddr;
Alexander Graf90a199e2016-04-11 16:16:16 +0200193
194 if (dev_desc->blksz == 512) {
195 info->size *= 4;
196 info->start *= 4;
197 info->blksz = 512;
198 }
199
200 PRINTF(" part %d found @ %lx size %lx\n",part_num,info->start,info->size);
wdenkcc1c8a12002-11-02 22:58:18 +0000201 return 0;
202}
203
Simon Glassb89a8442016-02-29 15:25:48 -0700204static int part_get_info_iso(struct blk_desc *dev_desc, int part_num,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600205 struct disk_partition *info)
wdenkcc1c8a12002-11-02 22:58:18 +0000206{
Alexander Graf6f57a862017-10-06 13:35:07 +0200207 return part_get_info_iso_verb(dev_desc, part_num, info, 0);
wdenkcc1c8a12002-11-02 22:58:18 +0000208}
209
Simon Glass0f1c9522016-02-29 15:26:04 -0700210static void part_print_iso(struct blk_desc *dev_desc)
wdenkcc1c8a12002-11-02 22:58:18 +0000211{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600212 struct disk_partition info;
wdenkcc1c8a12002-11-02 22:58:18 +0000213 int i;
Simon Glassb89a8442016-02-29 15:25:48 -0700214
Alexander Graf1f09d002016-07-21 01:31:56 +0200215 if (part_get_info_iso_verb(dev_desc, 1, &info, 0) == -1) {
Simon Glass2f26fff2016-02-29 15:25:51 -0700216 printf("** No boot partition found on device %d **\n",
217 dev_desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000218 return;
219 }
220 printf("Part Start Sect x Size Type\n");
Alexander Graf1f09d002016-07-21 01:31:56 +0200221 i=1;
wdenkcc1c8a12002-11-02 22:58:18 +0000222 do {
Heinrich Schuchardt4a5451c2021-08-06 00:51:46 +0200223 printf(" %2d %8" LBAFlength "u %8" LBAFlength "u %6ld %.32s\n",
Frederic Leroye7ee0282013-06-26 18:11:25 +0200224 i, info.start, info.size, info.blksz, info.type);
wdenkcc1c8a12002-11-02 22:58:18 +0000225 i++;
Simon Glassb89a8442016-02-29 15:25:48 -0700226 } while (part_get_info_iso_verb(dev_desc, i, &info, 0) != -1);
wdenkcc1c8a12002-11-02 22:58:18 +0000227}
228
Simon Glass0f1c9522016-02-29 15:26:04 -0700229static int part_test_iso(struct blk_desc *dev_desc)
wdenkcc1c8a12002-11-02 22:58:18 +0000230{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600231 struct disk_partition info;
wdenkcc1c8a12002-11-02 22:58:18 +0000232
Alexander Graf6f57a862017-10-06 13:35:07 +0200233 return part_get_info_iso_verb(dev_desc, 1, &info, 0);
wdenkcc1c8a12002-11-02 22:58:18 +0000234}
235
Simon Glass83ce5632016-02-29 15:25:47 -0700236U_BOOT_PART_TYPE(iso) = {
237 .name = "ISO",
238 .part_type = PART_TYPE_ISO,
Petr Kulhavy712257e2016-09-09 10:27:15 +0200239 .max_entries = ISO_ENTRY_NUMBERS,
Simon Glassb89a8442016-02-29 15:25:48 -0700240 .get_info = part_get_info_iso,
Simon Glass0f1c9522016-02-29 15:26:04 -0700241 .print = part_print_iso,
242 .test = part_test_iso,
Simon Glass83ce5632016-02-29 15:25:47 -0700243};
Jon Loeligerf7f54a82007-07-09 17:22:37 -0500244#endif