blob: e733227f5996b8ef83ccb4a1fa5ac1f7b02a8699 [file] [log] [blame]
Tom Rini83d290c2018-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>
8#include <command.h>
Simon Glass09192282016-03-16 07:45:34 -06009#include <asm/unaligned.h>
wdenkcc1c8a12002-11-02 22:58:18 +000010#include "part_iso.h"
11
Adam Ford1811a922018-02-06 12:43:56 -060012#ifdef CONFIG_HAVE_BLOCK_DEVICE
wdenkcc1c8a12002-11-02 22:58:18 +000013
wdenk1968e612005-02-24 23:23:29 +000014/* #define ISO_PART_DEBUG */
wdenkcc1c8a12002-11-02 22:58:18 +000015
16#ifdef ISO_PART_DEBUG
17#define PRINTF(fmt,args...) printf (fmt ,##args)
18#else
19#define PRINTF(fmt,args...)
20#endif
21
22/* enable this if CDs are written with the PowerPC Platform ID */
23#undef CHECK_FOR_POWERPC_PLATTFORM
24#define CD_SECTSIZE 2048
25
Stefan Agner5c275352017-08-23 09:46:17 -070026static unsigned char tmpbuf[CD_SECTSIZE] __aligned(ARCH_DMA_MINALIGN);
wdenkcc1c8a12002-11-02 22:58:18 +000027
Alexander Grafa2adb172016-04-11 16:16:16 +020028unsigned long iso_dread(struct blk_desc *block_dev, lbaint_t start,
29 lbaint_t blkcnt, void *buffer)
30{
31 unsigned long ret;
32
33 if (block_dev->blksz == 512) {
34 /* Convert from 2048 to 512 sector size */
35 start *= 4;
36 blkcnt *= 4;
37 }
38
39 ret = blk_dread(block_dev, start, blkcnt, buffer);
40
41 if (block_dev->blksz == 512)
42 ret /= 4;
43
44 return ret;
45}
46
wdenkcc1c8a12002-11-02 22:58:18 +000047/* only boot records will be listed as valid partitions */
Simon Glass3e8bd462016-02-29 15:25:48 -070048int part_get_info_iso_verb(struct blk_desc *dev_desc, int part_num,
49 disk_partition_t *info, int verb)
wdenkcc1c8a12002-11-02 22:58:18 +000050{
51 int i,offset,entry_num;
52 unsigned short *chksumbuf;
53 unsigned short chksum;
54 unsigned long newblkaddr,blkaddr,lastsect,bootaddr;
55 iso_boot_rec_t *pbr = (iso_boot_rec_t *)tmpbuf; /* boot record */
56 iso_pri_rec_t *ppr = (iso_pri_rec_t *)tmpbuf; /* primary desc */
57 iso_val_entry_t *pve = (iso_val_entry_t *)tmpbuf;
58 iso_init_def_entry_t *pide;
59
Alexander Grafa2adb172016-04-11 16:16:16 +020060 if ((dev_desc->blksz != CD_SECTSIZE) && (dev_desc->blksz != 512))
Egbert Eichd7ea4d42013-04-09 21:11:54 +000061 return -1;
62
wdenkcc1c8a12002-11-02 22:58:18 +000063 /* the first sector (sector 0x10) must be a primary volume desc */
64 blkaddr=PVD_OFFSET;
Alexander Grafa2adb172016-04-11 16:16:16 +020065 if (iso_dread(dev_desc, PVD_OFFSET, 1, (ulong *)tmpbuf) != 1)
Stephen Warren7c4213f2015-12-07 11:38:48 -070066 return -1;
wdenkcc1c8a12002-11-02 22:58:18 +000067 if(ppr->desctype!=0x01) {
68 if(verb)
69 printf ("** First descriptor is NOT a primary desc on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -070070 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +000071 return (-1);
72 }
Wolfgang Denk77ddac92005-10-13 16:45:02 +020073 if(strncmp((char *)ppr->stand_ident,"CD001",5)!=0) {
wdenkcc1c8a12002-11-02 22:58:18 +000074 if(verb)
75 printf ("** Wrong ISO Ident: %s on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -070076 ppr->stand_ident, dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +000077 return (-1);
78 }
Alexander Grafef9e6de2016-04-11 16:16:14 +020079 lastsect = le32_to_cpu(ppr->firstsek_LEpathtab1_LE);
80 /* assuming same block size for all entries */
81 info->blksz = be16_to_cpu(ppr->secsize_BE);
wdenkcc1c8a12002-11-02 22:58:18 +000082 PRINTF(" Lastsect:%08lx\n",lastsect);
83 for(i=blkaddr;i<lastsect;i++) {
wdenkc7de8292002-11-19 11:04:11 +000084 PRINTF("Reading block %d\n", i);
Alexander Grafa2adb172016-04-11 16:16:16 +020085 if (iso_dread(dev_desc, i, 1, (ulong *)tmpbuf) != 1)
Stephen Warren7c4213f2015-12-07 11:38:48 -070086 return -1;
wdenkcc1c8a12002-11-02 22:58:18 +000087 if(ppr->desctype==0x00)
88 break; /* boot entry found */
89 if(ppr->desctype==0xff) {
90 if(verb)
91 printf ("** No valid boot catalog found on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -070092 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +000093 return (-1);
94 }
95 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +020096 /* boot entry found */
wdenkcc1c8a12002-11-02 22:58:18 +000097 if(strncmp(pbr->ident_str,"EL TORITO SPECIFICATION",23)!=0) {
98 if(verb)
99 printf ("** Wrong El Torito ident: %s on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700100 pbr->ident_str, dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000101 return (-1);
102 }
Simon Glass09192282016-03-16 07:45:34 -0600103 bootaddr = get_unaligned_le32(pbr->pointer);
wdenkcc1c8a12002-11-02 22:58:18 +0000104 PRINTF(" Boot Entry at: %08lX\n",bootaddr);
Alexander Grafa2adb172016-04-11 16:16:16 +0200105 if (iso_dread(dev_desc, bootaddr, 1, (ulong *)tmpbuf) != 1) {
wdenkcc1c8a12002-11-02 22:58:18 +0000106 if(verb)
107 printf ("** Can't read Boot Entry at %lX on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700108 bootaddr, dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000109 return (-1);
110 }
111 chksum=0;
112 chksumbuf = (unsigned short *)tmpbuf;
113 for(i=0;i<0x10;i++)
Alexander Grafef9e6de2016-04-11 16:16:14 +0200114 chksum += le16_to_cpu(chksumbuf[i]);
wdenkcc1c8a12002-11-02 22:58:18 +0000115 if(chksum!=0) {
116 if(verb)
Simon Glassbcce53d2016-02-29 15:25:51 -0700117 printf("** Checksum Error in booting catalog validation entry on %d:%d **\n",
118 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000119 return (-1);
120 }
121 if((pve->key[0]!=0x55)||(pve->key[1]!=0xAA)) {
122 if(verb)
123 printf ("** Key 0x55 0xAA error on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700124 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000125 return(-1);
126 }
127#ifdef CHECK_FOR_POWERPC_PLATTFORM
128 if(pve->platform!=0x01) {
129 if(verb)
130 printf ("** No PowerPC platform CD on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700131 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000132 return(-1);
133 }
134#endif
135 /* the validation entry seems to be ok, now search the "partition" */
Alexander Graf2579c672016-04-11 16:16:15 +0200136 entry_num=1;
wdenkcc1c8a12002-11-02 22:58:18 +0000137 offset=0x20;
Ben Whitten192bc692015-12-30 13:05:58 +0000138 strcpy((char *)info->type, "U-Boot");
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200139 part_set_generic_name(dev_desc, part_num, (char *)info->name);
wdenkcc1c8a12002-11-02 22:58:18 +0000140 /* the bootcatalog (including validation Entry) is limited to 2048Bytes
141 * (63 boot entries + validation entry) */
142 while(offset<2048) {
143 pide=(iso_init_def_entry_t *)&tmpbuf[offset];
144 if ((pide->boot_ind==0x88) ||
145 (pide->boot_ind==0x00)) { /* Header Id for default Sections Entries */
146 if(entry_num==part_num) { /* part found */
147 goto found;
148 }
149 entry_num++; /* count partitions Entries (boot and non bootables */
150 offset+=0x20;
151 continue;
152 }
153 if ((pide->boot_ind==0x90) || /* Section Header Entry */
154 (pide->boot_ind==0x91) || /* Section Header Entry (last) */
155 (pide->boot_ind==0x44)) { /* Extension Indicator */
156 offset+=0x20; /* skip unused entries */
157 }
158 else {
159 if(verb)
160 printf ("** Partition %d not found on device %d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700161 part_num, dev_desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000162 return(-1);
163 }
164 }
165 /* if we reach this point entire sector has been
166 * searched w/o succsess */
167 if(verb)
168 printf ("** Partition %d not found on device %d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700169 part_num, dev_desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000170 return(-1);
171found:
172 if(pide->boot_ind!=0x88) {
173 if(verb)
Simon Glassbcce53d2016-02-29 15:25:51 -0700174 printf("** Partition %d is not bootable on device %d **\n",
175 part_num, dev_desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000176 return (-1);
177 }
178 switch(pide->boot_media) {
179 case 0x00: /* no emulation */
Simon Glass09192282016-03-16 07:45:34 -0600180 info->size = get_unaligned_le16(pide->sec_cnt)>>2;
wdenkcc1c8a12002-11-02 22:58:18 +0000181 break;
182 case 0x01: info->size=2400>>2; break; /* 1.2MByte Floppy */
183 case 0x02: info->size=2880>>2; break; /* 1.44MByte Floppy */
184 case 0x03: info->size=5760>>2; break; /* 2.88MByte Floppy */
185 case 0x04: info->size=2880>>2; break; /* dummy (HD Emulation) */
186 default: info->size=0; break;
187 }
Simon Glass09192282016-03-16 07:45:34 -0600188 newblkaddr = get_unaligned_le32(pide->rel_block_addr);
wdenkcc1c8a12002-11-02 22:58:18 +0000189 info->start=newblkaddr;
Alexander Grafa2adb172016-04-11 16:16:16 +0200190
191 if (dev_desc->blksz == 512) {
192 info->size *= 4;
193 info->start *= 4;
194 info->blksz = 512;
195 }
196
197 PRINTF(" part %d found @ %lx size %lx\n",part_num,info->start,info->size);
wdenkcc1c8a12002-11-02 22:58:18 +0000198 return 0;
199}
200
Simon Glass3e8bd462016-02-29 15:25:48 -0700201static int part_get_info_iso(struct blk_desc *dev_desc, int part_num,
Simon Glass96e5b032016-02-29 15:25:47 -0700202 disk_partition_t *info)
wdenkcc1c8a12002-11-02 22:58:18 +0000203{
Alexander Grafb16339e2017-10-06 13:35:07 +0200204 return part_get_info_iso_verb(dev_desc, part_num, info, 0);
wdenkcc1c8a12002-11-02 22:58:18 +0000205}
206
Simon Glass084bf4c2016-02-29 15:26:04 -0700207static void part_print_iso(struct blk_desc *dev_desc)
wdenkcc1c8a12002-11-02 22:58:18 +0000208{
209 disk_partition_t info;
210 int i;
Simon Glass3e8bd462016-02-29 15:25:48 -0700211
Alexander Graf28f00142016-07-21 01:31:56 +0200212 if (part_get_info_iso_verb(dev_desc, 1, &info, 0) == -1) {
Simon Glassbcce53d2016-02-29 15:25:51 -0700213 printf("** No boot partition found on device %d **\n",
214 dev_desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000215 return;
216 }
217 printf("Part Start Sect x Size Type\n");
Alexander Graf28f00142016-07-21 01:31:56 +0200218 i=1;
wdenkcc1c8a12002-11-02 22:58:18 +0000219 do {
Frederic Leroy04735e92013-06-26 18:11:25 +0200220 printf(" %2d " LBAFU " " LBAFU " %6ld %.32s\n",
221 i, info.start, info.size, info.blksz, info.type);
wdenkcc1c8a12002-11-02 22:58:18 +0000222 i++;
Simon Glass3e8bd462016-02-29 15:25:48 -0700223 } while (part_get_info_iso_verb(dev_desc, i, &info, 0) != -1);
wdenkcc1c8a12002-11-02 22:58:18 +0000224}
225
Simon Glass084bf4c2016-02-29 15:26:04 -0700226static int part_test_iso(struct blk_desc *dev_desc)
wdenkcc1c8a12002-11-02 22:58:18 +0000227{
228 disk_partition_t info;
229
Alexander Grafb16339e2017-10-06 13:35:07 +0200230 return part_get_info_iso_verb(dev_desc, 1, &info, 0);
wdenkcc1c8a12002-11-02 22:58:18 +0000231}
232
Simon Glass96e5b032016-02-29 15:25:47 -0700233U_BOOT_PART_TYPE(iso) = {
234 .name = "ISO",
235 .part_type = PART_TYPE_ISO,
Petr Kulhavy87b85302016-09-09 10:27:15 +0200236 .max_entries = ISO_ENTRY_NUMBERS,
Simon Glass3e8bd462016-02-29 15:25:48 -0700237 .get_info = part_get_info_iso,
Simon Glass084bf4c2016-02-29 15:26:04 -0700238 .print = part_print_iso,
239 .test = part_test_iso,
Simon Glass96e5b032016-02-29 15:25:47 -0700240};
Jon Loeligercde5c642007-07-09 17:22:37 -0500241#endif