Fix IA32 build failure: Use MultU64x32 for 64bit * 32bit.
[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
173acde9 4Copyright (c) 2006 - 2015, 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
47 EFI_STATUS Status;\r
5d6bf9e2
OM
48 UINT64 VolDescriptorOffset;\r
49 UINT32 Lba2KB;\r
adbcbf8f 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\r
64 Found = EFI_NOT_FOUND;\r
65 Media = BlockIo->Media;\r
490b5ea1 66\r
adbcbf8f 67 VolSpaceSize = 0;\r
68\r
69 //\r
5d6bf9e2 70 // CD_ROM has the fixed block size as 2048 bytes (SIZE_2KB)\r
adbcbf8f 71 //\r
5d6bf9e2
OM
72\r
73 // If the ISO image has been copied onto a different storage media\r
74 // then the block size might be different (eg: USB).\r
75 // Ensure 2048 (SIZE_2KB) is a multiple of block size\r
76 if (((SIZE_2KB % Media->BlockSize) != 0) || (Media->BlockSize > SIZE_2KB)) {\r
adbcbf8f 77 return EFI_NOT_FOUND;\r
78 }\r
79\r
5d6bf9e2 80 VolDescriptor = AllocatePool ((UINTN)SIZE_2KB);\r
adbcbf8f 81\r
82 if (VolDescriptor == NULL) {\r
83 return EFI_NOT_FOUND;\r
84 }\r
85\r
86 Catalog = (ELTORITO_CATALOG *) VolDescriptor;\r
87\r
adbcbf8f 88 //\r
89 // Loop: handle one volume descriptor per time\r
5d6bf9e2 90 // The ISO-9660 volume descriptor starts at 32k on the media\r
adbcbf8f 91 //\r
5d6bf9e2
OM
92 for (VolDescriptorOffset = SIZE_32KB;\r
93 VolDescriptorOffset <= MultU64x32 (Media->LastBlock, Media->BlockSize);\r
94 VolDescriptorOffset += SIZE_2KB) {\r
96f99e1d 95 Status = DiskIo->ReadDisk (\r
96 DiskIo,\r
97 Media->MediaId,\r
5d6bf9e2
OM
98 VolDescriptorOffset,\r
99 SIZE_2KB,\r
96f99e1d 100 VolDescriptor\r
101 );\r
adbcbf8f 102 if (EFI_ERROR (Status)) {\r
103 Found = Status;\r
104 break;\r
105 }\r
106 //\r
107 // Check for valid volume descriptor signature\r
108 //\r
88a19367 109 if (VolDescriptor->Unknown.Type == CDVOL_TYPE_END ||\r
110 CompareMem (VolDescriptor->Unknown.Id, CDVOL_ID, sizeof (VolDescriptor->Unknown.Id)) != 0\r
adbcbf8f 111 ) {\r
112 //\r
113 // end of Volume descriptor list\r
114 //\r
115 break;\r
116 }\r
117 //\r
118 // Read the Volume Space Size from Primary Volume Descriptor 81-88 byte,\r
119 // the 32-bit numerical values is stored in Both-byte orders\r
120 //\r
88a19367 121 if (VolDescriptor->PrimaryVolume.Type == CDVOL_TYPE_CODED) {\r
122 VolSpaceSize = VolDescriptor->PrimaryVolume.VolSpaceSize[0];\r
adbcbf8f 123 }\r
124 //\r
125 // Is it an El Torito volume descriptor?\r
126 //\r
88a19367 127 if (CompareMem (VolDescriptor->BootRecordVolume.SystemId, CDVOL_ELTORITO_ID, sizeof (CDVOL_ELTORITO_ID) - 1) != 0) {\r
adbcbf8f 128 continue;\r
129 }\r
130 //\r
131 // Read in the boot El Torito boot catalog\r
5d6bf9e2 132 // The LBA unit used by El Torito boot catalog is 2KB unit\r
adbcbf8f 133 //\r
5d6bf9e2
OM
134 Lba2KB = UNPACK_INT32 (VolDescriptor->BootRecordVolume.EltCatalog);\r
135 // Ensure the LBA (in 2KB unit) fits into our media\r
136 if (Lba2KB * (SIZE_2KB / Media->BlockSize) > Media->LastBlock) {\r
adbcbf8f 137 continue;\r
138 }\r
139\r
96f99e1d 140 Status = DiskIo->ReadDisk (\r
141 DiskIo,\r
142 Media->MediaId,\r
5d6bf9e2
OM
143 MultU64x32 (Lba2KB, SIZE_2KB),\r
144 SIZE_2KB,\r
96f99e1d 145 Catalog\r
146 );\r
adbcbf8f 147 if (EFI_ERROR (Status)) {\r
148 DEBUG ((EFI_D_ERROR, "EltCheckDevice: error reading catalog %r\n", Status));\r
149 continue;\r
150 }\r
151 //\r
152 // We don't care too much about the Catalog header's contents, but we do want\r
153 // to make sure it looks like a Catalog header\r
154 //\r
155 if (Catalog->Catalog.Indicator != ELTORITO_ID_CATALOG || Catalog->Catalog.Id55AA != 0xAA55) {\r
156 DEBUG ((EFI_D_ERROR, "EltCheckBootCatalog: El Torito boot catalog header IDs not correct\n"));\r
157 continue;\r
158 }\r
159\r
160 Check = 0;\r
161 CheckBuffer = (UINT16 *) Catalog;\r
162 for (Index = 0; Index < sizeof (ELTORITO_CATALOG) / sizeof (UINT16); Index += 1) {\r
163 Check += CheckBuffer[Index];\r
164 }\r
165\r
ea7cb08c 166 if ((Check & 0xFFFF) != 0) {\r
adbcbf8f 167 DEBUG ((EFI_D_ERROR, "EltCheckBootCatalog: El Torito boot catalog header checksum failed\n"));\r
168 continue;\r
169 }\r
170\r
171 MaxIndex = Media->BlockSize / sizeof (ELTORITO_CATALOG);\r
172 for (Index = 1, BootEntry = 1; Index < MaxIndex; Index += 1) {\r
173 //\r
174 // Next entry\r
175 //\r
176 Catalog += 1;\r
177\r
178 //\r
179 // Check this entry\r
180 //\r
181 if (Catalog->Boot.Indicator != ELTORITO_ID_SECTION_BOOTABLE || Catalog->Boot.Lba == 0) {\r
182 continue;\r
183 }\r
184\r
185 SubBlockSize = 512;\r
5d6bf9e2 186 SectorCount = Catalog->Boot.SectorCount * (SIZE_2KB / Media->BlockSize);\r
adbcbf8f 187\r
188 switch (Catalog->Boot.MediaType) {\r
189\r
190 case ELTORITO_NO_EMULATION:\r
191 SubBlockSize = Media->BlockSize;\r
192 break;\r
193\r
194 case ELTORITO_HARD_DISK:\r
195 break;\r
196\r
197 case ELTORITO_12_DISKETTE:\r
198 SectorCount = 0x50 * 0x02 * 0x0F;\r
199 break;\r
200\r
201 case ELTORITO_14_DISKETTE:\r
202 SectorCount = 0x50 * 0x02 * 0x12;\r
203 break;\r
204\r
205 case ELTORITO_28_DISKETTE:\r
206 SectorCount = 0x50 * 0x02 * 0x24;\r
207 break;\r
208\r
209 default:\r
210 DEBUG ((EFI_D_INIT, "EltCheckDevice: unsupported El Torito boot media type %x\n", Catalog->Boot.MediaType));\r
211 SectorCount = 0;\r
212 SubBlockSize = Media->BlockSize;\r
213 break;\r
214 }\r
215 //\r
216 // Create child device handle\r
217 //\r
218 CdDev.Header.Type = MEDIA_DEVICE_PATH;\r
219 CdDev.Header.SubType = MEDIA_CDROM_DP;\r
220 SetDevicePathNodeLength (&CdDev.Header, sizeof (CdDev));\r
221\r
222 if (Index == 1) {\r
223 //\r
224 // This is the initial/default entry\r
225 //\r
226 BootEntry = 0;\r
227 }\r
228\r
229 CdDev.BootEntry = (UINT32) BootEntry;\r
230 BootEntry++;\r
5d6bf9e2 231 CdDev.PartitionStart = Catalog->Boot.Lba * (SIZE_2KB / Media->BlockSize);\r
adbcbf8f 232 if (SectorCount < 2) {\r
233 //\r
234 // When the SectorCount < 2, set the Partition as the whole CD.\r
235 //\r
236 if (VolSpaceSize > (Media->LastBlock + 1)) {\r
237 CdDev.PartitionSize = (UINT32)(Media->LastBlock - Catalog->Boot.Lba + 1);\r
238 } else {\r
239 CdDev.PartitionSize = (UINT32)(VolSpaceSize - Catalog->Boot.Lba);\r
240 }\r
241 } else {\r
242 CdDev.PartitionSize = DivU64x32 (\r
243 MultU64x32 (\r
244 SectorCount,\r
245 SubBlockSize\r
246 ) + Media->BlockSize - 1,\r
247 Media->BlockSize\r
248 );\r
249 }\r
250\r
251 Status = PartitionInstallChildHandle (\r
252 This,\r
253 Handle,\r
254 DiskIo,\r
493d8e3a 255 DiskIo2,\r
adbcbf8f 256 BlockIo,\r
490b5ea1 257 BlockIo2,\r
adbcbf8f 258 DevicePath,\r
259 (EFI_DEVICE_PATH_PROTOCOL *) &CdDev,\r
5d6bf9e2 260 Catalog->Boot.Lba * (SIZE_2KB / Media->BlockSize),\r
173acde9 261 MultU64x32 (Catalog->Boot.Lba + CdDev.PartitionSize - 1, SIZE_2KB / Media->BlockSize),\r
adbcbf8f 262 SubBlockSize,\r
263 FALSE\r
264 );\r
265 if (!EFI_ERROR (Status)) {\r
266 Found = EFI_SUCCESS;\r
267 }\r
268 }\r
269 }\r
270\r
271 FreePool (VolDescriptor);\r
272\r
273 return Found;\r
274}\r