]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Disk/PartitionDxe/ElTorito.c
MdeModulePkg/UdfDxe: Fix operands of different size in bitwise OP
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / PartitionDxe / ElTorito.c
CommitLineData
adbcbf8f 1/** @file\r
2 Decode an El Torito formatted CD-ROM\r
3\r
3a3d62d2 4Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 5This program and the accompanying materials\r
f42be642 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
adbcbf8f 12\r
13**/\r
14\r
15\r
16#include "Partition.h"\r
17\r
18\r
19/**\r
20 Install child handles if the Handle supports El Torito format.\r
21\r
22 @param[in] This Calling context.\r
490b5ea1 23 @param[in] Handle Parent Handle.\r
24 @param[in] DiskIo Parent DiskIo interface.\r
493d8e3a 25 @param[in] DiskIo2 Parent DiskIo2 interface.\r
490b5ea1 26 @param[in] BlockIo Parent BlockIo interface.\r
27 @param[in] BlockIo2 Parent BlockIo2 interface.\r
adbcbf8f 28 @param[in] DevicePath Parent Device Path\r
29\r
30\r
490b5ea1 31 @retval EFI_SUCCESS Child handle(s) was added.\r
32 @retval EFI_MEDIA_CHANGED Media changed Detected.\r
33 @retval other no child handle was added.\r
adbcbf8f 34\r
35**/\r
36EFI_STATUS\r
37PartitionInstallElToritoChildHandles (\r
38 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
39 IN EFI_HANDLE Handle,\r
40 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
493d8e3a 41 IN EFI_DISK_IO2_PROTOCOL *DiskIo2,\r
adbcbf8f 42 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
490b5ea1 43 IN EFI_BLOCK_IO2_PROTOCOL *BlockIo2,\r
adbcbf8f 44 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
45 )\r
46{\r
3a3d62d2
HW
47 EFI_STATUS Status;\r
48 UINT64 VolDescriptorOffset;\r
49 UINT32 Lba2KB;\r
50 EFI_BLOCK_IO_MEDIA *Media;\r
51 CDROM_VOLUME_DESCRIPTOR *VolDescriptor;\r
52 ELTORITO_CATALOG *Catalog;\r
53 UINTN Check;\r
54 UINTN Index;\r
55 UINTN BootEntry;\r
56 UINTN MaxIndex;\r
57 UINT16 *CheckBuffer;\r
58 CDROM_DEVICE_PATH CdDev;\r
59 UINT32 SubBlockSize;\r
60 UINT32 SectorCount;\r
61 EFI_STATUS Found;\r
62 UINT32 VolSpaceSize;\r
63 EFI_PARTITION_INFO_PROTOCOL PartitionInfo;\r
adbcbf8f 64\r
65 Found = EFI_NOT_FOUND;\r
66 Media = BlockIo->Media;\r
490b5ea1 67\r
adbcbf8f 68 VolSpaceSize = 0;\r
69\r
70 //\r
5d6bf9e2 71 // CD_ROM has the fixed block size as 2048 bytes (SIZE_2KB)\r
adbcbf8f 72 //\r
5d6bf9e2
OM
73\r
74 // If the ISO image has been copied onto a different storage media\r
75 // then the block size might be different (eg: USB).\r
76 // Ensure 2048 (SIZE_2KB) is a multiple of block size\r
77 if (((SIZE_2KB % Media->BlockSize) != 0) || (Media->BlockSize > SIZE_2KB)) {\r
adbcbf8f 78 return EFI_NOT_FOUND;\r
79 }\r
80\r
5d6bf9e2 81 VolDescriptor = AllocatePool ((UINTN)SIZE_2KB);\r
adbcbf8f 82\r
83 if (VolDescriptor == NULL) {\r
84 return EFI_NOT_FOUND;\r
85 }\r
86\r
87 Catalog = (ELTORITO_CATALOG *) VolDescriptor;\r
88\r
adbcbf8f 89 //\r
90 // Loop: handle one volume descriptor per time\r
5d6bf9e2 91 // The ISO-9660 volume descriptor starts at 32k on the media\r
adbcbf8f 92 //\r
5d6bf9e2
OM
93 for (VolDescriptorOffset = SIZE_32KB;\r
94 VolDescriptorOffset <= MultU64x32 (Media->LastBlock, Media->BlockSize);\r
95 VolDescriptorOffset += SIZE_2KB) {\r
96f99e1d 96 Status = DiskIo->ReadDisk (\r
97 DiskIo,\r
98 Media->MediaId,\r
5d6bf9e2
OM
99 VolDescriptorOffset,\r
100 SIZE_2KB,\r
96f99e1d 101 VolDescriptor\r
102 );\r
adbcbf8f 103 if (EFI_ERROR (Status)) {\r
104 Found = Status;\r
105 break;\r
106 }\r
107 //\r
108 // Check for valid volume descriptor signature\r
109 //\r
88a19367 110 if (VolDescriptor->Unknown.Type == CDVOL_TYPE_END ||\r
111 CompareMem (VolDescriptor->Unknown.Id, CDVOL_ID, sizeof (VolDescriptor->Unknown.Id)) != 0\r
adbcbf8f 112 ) {\r
113 //\r
114 // end of Volume descriptor list\r
115 //\r
116 break;\r
117 }\r
118 //\r
119 // Read the Volume Space Size from Primary Volume Descriptor 81-88 byte,\r
120 // the 32-bit numerical values is stored in Both-byte orders\r
121 //\r
88a19367 122 if (VolDescriptor->PrimaryVolume.Type == CDVOL_TYPE_CODED) {\r
123 VolSpaceSize = VolDescriptor->PrimaryVolume.VolSpaceSize[0];\r
adbcbf8f 124 }\r
125 //\r
126 // Is it an El Torito volume descriptor?\r
127 //\r
88a19367 128 if (CompareMem (VolDescriptor->BootRecordVolume.SystemId, CDVOL_ELTORITO_ID, sizeof (CDVOL_ELTORITO_ID) - 1) != 0) {\r
adbcbf8f 129 continue;\r
130 }\r
131 //\r
132 // Read in the boot El Torito boot catalog\r
5d6bf9e2 133 // The LBA unit used by El Torito boot catalog is 2KB unit\r
adbcbf8f 134 //\r
5d6bf9e2
OM
135 Lba2KB = UNPACK_INT32 (VolDescriptor->BootRecordVolume.EltCatalog);\r
136 // Ensure the LBA (in 2KB unit) fits into our media\r
137 if (Lba2KB * (SIZE_2KB / Media->BlockSize) > Media->LastBlock) {\r
adbcbf8f 138 continue;\r
139 }\r
140\r
96f99e1d 141 Status = DiskIo->ReadDisk (\r
142 DiskIo,\r
143 Media->MediaId,\r
5d6bf9e2
OM
144 MultU64x32 (Lba2KB, SIZE_2KB),\r
145 SIZE_2KB,\r
96f99e1d 146 Catalog\r
147 );\r
adbcbf8f 148 if (EFI_ERROR (Status)) {\r
149 DEBUG ((EFI_D_ERROR, "EltCheckDevice: error reading catalog %r\n", Status));\r
150 continue;\r
151 }\r
152 //\r
153 // We don't care too much about the Catalog header's contents, but we do want\r
154 // to make sure it looks like a Catalog header\r
155 //\r
156 if (Catalog->Catalog.Indicator != ELTORITO_ID_CATALOG || Catalog->Catalog.Id55AA != 0xAA55) {\r
157 DEBUG ((EFI_D_ERROR, "EltCheckBootCatalog: El Torito boot catalog header IDs not correct\n"));\r
158 continue;\r
159 }\r
160\r
161 Check = 0;\r
162 CheckBuffer = (UINT16 *) Catalog;\r
163 for (Index = 0; Index < sizeof (ELTORITO_CATALOG) / sizeof (UINT16); Index += 1) {\r
164 Check += CheckBuffer[Index];\r
165 }\r
166\r
ea7cb08c 167 if ((Check & 0xFFFF) != 0) {\r
adbcbf8f 168 DEBUG ((EFI_D_ERROR, "EltCheckBootCatalog: El Torito boot catalog header checksum failed\n"));\r
169 continue;\r
170 }\r
171\r
172 MaxIndex = Media->BlockSize / sizeof (ELTORITO_CATALOG);\r
173 for (Index = 1, BootEntry = 1; Index < MaxIndex; Index += 1) {\r
174 //\r
175 // Next entry\r
176 //\r
177 Catalog += 1;\r
178\r
179 //\r
180 // Check this entry\r
181 //\r
182 if (Catalog->Boot.Indicator != ELTORITO_ID_SECTION_BOOTABLE || Catalog->Boot.Lba == 0) {\r
183 continue;\r
184 }\r
185\r
186 SubBlockSize = 512;\r
744c7168 187 SectorCount = Catalog->Boot.SectorCount;\r
adbcbf8f 188\r
189 switch (Catalog->Boot.MediaType) {\r
190\r
191 case ELTORITO_NO_EMULATION:\r
192 SubBlockSize = Media->BlockSize;\r
193 break;\r
194\r
195 case ELTORITO_HARD_DISK:\r
196 break;\r
197\r
198 case ELTORITO_12_DISKETTE:\r
199 SectorCount = 0x50 * 0x02 * 0x0F;\r
200 break;\r
201\r
202 case ELTORITO_14_DISKETTE:\r
203 SectorCount = 0x50 * 0x02 * 0x12;\r
204 break;\r
205\r
206 case ELTORITO_28_DISKETTE:\r
207 SectorCount = 0x50 * 0x02 * 0x24;\r
208 break;\r
209\r
210 default:\r
211 DEBUG ((EFI_D_INIT, "EltCheckDevice: unsupported El Torito boot media type %x\n", Catalog->Boot.MediaType));\r
212 SectorCount = 0;\r
213 SubBlockSize = Media->BlockSize;\r
214 break;\r
215 }\r
216 //\r
217 // Create child device handle\r
218 //\r
219 CdDev.Header.Type = MEDIA_DEVICE_PATH;\r
220 CdDev.Header.SubType = MEDIA_CDROM_DP;\r
221 SetDevicePathNodeLength (&CdDev.Header, sizeof (CdDev));\r
222\r
223 if (Index == 1) {\r
224 //\r
225 // This is the initial/default entry\r
226 //\r
227 BootEntry = 0;\r
228 }\r
229\r
230 CdDev.BootEntry = (UINT32) BootEntry;\r
231 BootEntry++;\r
5d6bf9e2 232 CdDev.PartitionStart = Catalog->Boot.Lba * (SIZE_2KB / Media->BlockSize);\r
adbcbf8f 233 if (SectorCount < 2) {\r
234 //\r
235 // When the SectorCount < 2, set the Partition as the whole CD.\r
236 //\r
744c7168
HW
237 if (VolSpaceSize * (SIZE_2KB / Media->BlockSize) > (Media->LastBlock + 1)) {\r
238 CdDev.PartitionSize = (UINT32)(Media->LastBlock - Catalog->Boot.Lba * (SIZE_2KB / Media->BlockSize) + 1);\r
adbcbf8f 239 } else {\r
744c7168 240 CdDev.PartitionSize = (UINT32)(VolSpaceSize - Catalog->Boot.Lba) * (SIZE_2KB / Media->BlockSize);\r
adbcbf8f 241 }\r
242 } else {\r
243 CdDev.PartitionSize = DivU64x32 (\r
244 MultU64x32 (\r
744c7168 245 SectorCount * (SIZE_2KB / Media->BlockSize),\r
adbcbf8f 246 SubBlockSize\r
247 ) + Media->BlockSize - 1,\r
248 Media->BlockSize\r
249 );\r
250 }\r
251\r
3a3d62d2
HW
252 ZeroMem (&PartitionInfo, sizeof (EFI_PARTITION_INFO_PROTOCOL));\r
253 PartitionInfo.Revision = EFI_PARTITION_INFO_PROTOCOL_REVISION;\r
254 PartitionInfo.Type = PARTITION_TYPE_OTHER;\r
255\r
adbcbf8f 256 Status = PartitionInstallChildHandle (\r
257 This,\r
258 Handle,\r
259 DiskIo,\r
493d8e3a 260 DiskIo2,\r
adbcbf8f 261 BlockIo,\r
490b5ea1 262 BlockIo2,\r
adbcbf8f 263 DevicePath,\r
264 (EFI_DEVICE_PATH_PROTOCOL *) &CdDev,\r
3a3d62d2 265 &PartitionInfo,\r
5d6bf9e2 266 Catalog->Boot.Lba * (SIZE_2KB / Media->BlockSize),\r
744c7168 267 Catalog->Boot.Lba * (SIZE_2KB / Media->BlockSize) + CdDev.PartitionSize - 1,\r
3a3d62d2 268 SubBlockSize\r
adbcbf8f 269 );\r
270 if (!EFI_ERROR (Status)) {\r
271 Found = EFI_SUCCESS;\r
272 }\r
273 }\r
274 }\r
275\r
276 FreePool (VolDescriptor);\r
277\r
278 return Found;\r
279}\r