Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Helper functions used by the EFI stub on multiple |
| 3 | * architectures. This should be #included by the EFI stub |
| 4 | * implementation files. |
| 5 | * |
| 6 | * Copyright 2011 Intel Corporation; author Matt Fleming |
| 7 | * |
| 8 | * This file is part of the Linux kernel, and is made available |
| 9 | * under the terms of the GNU General Public License version 2. |
| 10 | * |
| 11 | */ |
| 12 | #define EFI_READ_CHUNK_SIZE (1024 * 1024) |
| 13 | |
| 14 | struct initrd { |
| 15 | efi_file_handle_t *handle; |
| 16 | u64 size; |
| 17 | }; |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 22 | static void efi_char16_printk(efi_system_table_t *sys_table_arg, |
| 23 | efi_char16_t *str) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 24 | { |
| 25 | struct efi_simple_text_output_protocol *out; |
| 26 | |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 27 | out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 28 | efi_call_phys2(out->output_string, out, str); |
| 29 | } |
| 30 | |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 31 | static void efi_printk(efi_system_table_t *sys_table_arg, char *str) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 32 | { |
| 33 | char *s8; |
| 34 | |
| 35 | for (s8 = str; *s8; s8++) { |
| 36 | efi_char16_t ch[2] = { 0 }; |
| 37 | |
| 38 | ch[0] = *s8; |
| 39 | if (*s8 == '\n') { |
| 40 | efi_char16_t nl[2] = { '\r', 0 }; |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 41 | efi_char16_printk(sys_table_arg, nl); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 44 | efi_char16_printk(sys_table_arg, ch); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 49 | static efi_status_t __get_map(efi_system_table_t *sys_table_arg, |
| 50 | efi_memory_desc_t **map, |
| 51 | unsigned long *map_size, |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 52 | unsigned long *desc_size) |
| 53 | { |
| 54 | efi_memory_desc_t *m = NULL; |
| 55 | efi_status_t status; |
| 56 | unsigned long key; |
| 57 | u32 desc_version; |
| 58 | |
| 59 | *map_size = sizeof(*m) * 32; |
| 60 | again: |
| 61 | /* |
| 62 | * Add an additional efi_memory_desc_t because we're doing an |
| 63 | * allocation which may be in a new descriptor region. |
| 64 | */ |
| 65 | *map_size += sizeof(*m); |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 66 | status = efi_call_phys3(sys_table_arg->boottime->allocate_pool, |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 67 | EFI_LOADER_DATA, *map_size, (void **)&m); |
| 68 | if (status != EFI_SUCCESS) |
| 69 | goto fail; |
| 70 | |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 71 | status = efi_call_phys5(sys_table_arg->boottime->get_memory_map, |
| 72 | map_size, m, &key, desc_size, &desc_version); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 73 | if (status == EFI_BUFFER_TOO_SMALL) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 74 | efi_call_phys1(sys_table_arg->boottime->free_pool, m); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 75 | goto again; |
| 76 | } |
| 77 | |
| 78 | if (status != EFI_SUCCESS) |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 79 | efi_call_phys1(sys_table_arg->boottime->free_pool, m); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 80 | |
| 81 | fail: |
| 82 | *map = m; |
| 83 | return status; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Allocate at the highest possible address that is not above 'max'. |
| 88 | */ |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 89 | static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg, |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 90 | unsigned long size, unsigned long align, |
| 91 | unsigned long *addr, unsigned long max) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 92 | { |
| 93 | unsigned long map_size, desc_size; |
| 94 | efi_memory_desc_t *map; |
| 95 | efi_status_t status; |
| 96 | unsigned long nr_pages; |
| 97 | u64 max_addr = 0; |
| 98 | int i; |
| 99 | |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 100 | status = __get_map(sys_table_arg, &map, &map_size, &desc_size); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 101 | if (status != EFI_SUCCESS) |
| 102 | goto fail; |
| 103 | |
Roy Franz | 38dd9c0 | 2013-09-22 15:45:30 -0700 | [diff] [blame^] | 104 | /* |
| 105 | * Enforce minimum alignment that EFI requires when requesting |
| 106 | * a specific address. We are doing page-based allocations, |
| 107 | * so we must be aligned to a page. |
| 108 | */ |
| 109 | if (align < EFI_PAGE_SIZE) |
| 110 | align = EFI_PAGE_SIZE; |
| 111 | |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 112 | nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; |
| 113 | again: |
| 114 | for (i = 0; i < map_size / desc_size; i++) { |
| 115 | efi_memory_desc_t *desc; |
| 116 | unsigned long m = (unsigned long)map; |
| 117 | u64 start, end; |
| 118 | |
| 119 | desc = (efi_memory_desc_t *)(m + (i * desc_size)); |
| 120 | if (desc->type != EFI_CONVENTIONAL_MEMORY) |
| 121 | continue; |
| 122 | |
| 123 | if (desc->num_pages < nr_pages) |
| 124 | continue; |
| 125 | |
| 126 | start = desc->phys_addr; |
| 127 | end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT); |
| 128 | |
| 129 | if ((start + size) > end || (start + size) > max) |
| 130 | continue; |
| 131 | |
| 132 | if (end - size > max) |
| 133 | end = max; |
| 134 | |
| 135 | if (round_down(end - size, align) < start) |
| 136 | continue; |
| 137 | |
| 138 | start = round_down(end - size, align); |
| 139 | |
| 140 | /* |
| 141 | * Don't allocate at 0x0. It will confuse code that |
| 142 | * checks pointers against NULL. |
| 143 | */ |
| 144 | if (start == 0x0) |
| 145 | continue; |
| 146 | |
| 147 | if (start > max_addr) |
| 148 | max_addr = start; |
| 149 | } |
| 150 | |
| 151 | if (!max_addr) |
| 152 | status = EFI_NOT_FOUND; |
| 153 | else { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 154 | status = efi_call_phys4(sys_table_arg->boottime->allocate_pages, |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 155 | EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, |
| 156 | nr_pages, &max_addr); |
| 157 | if (status != EFI_SUCCESS) { |
| 158 | max = max_addr; |
| 159 | max_addr = 0; |
| 160 | goto again; |
| 161 | } |
| 162 | |
| 163 | *addr = max_addr; |
| 164 | } |
| 165 | |
| 166 | free_pool: |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 167 | efi_call_phys1(sys_table_arg->boottime->free_pool, map); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 168 | |
| 169 | fail: |
| 170 | return status; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Allocate at the lowest possible address. |
| 175 | */ |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 176 | static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg, |
| 177 | unsigned long size, unsigned long align, |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 178 | unsigned long *addr) |
| 179 | { |
| 180 | unsigned long map_size, desc_size; |
| 181 | efi_memory_desc_t *map; |
| 182 | efi_status_t status; |
| 183 | unsigned long nr_pages; |
| 184 | int i; |
| 185 | |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 186 | status = __get_map(sys_table_arg, &map, &map_size, &desc_size); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 187 | if (status != EFI_SUCCESS) |
| 188 | goto fail; |
| 189 | |
Roy Franz | 38dd9c0 | 2013-09-22 15:45:30 -0700 | [diff] [blame^] | 190 | /* |
| 191 | * Enforce minimum alignment that EFI requires when requesting |
| 192 | * a specific address. We are doing page-based allocations, |
| 193 | * so we must be aligned to a page. |
| 194 | */ |
| 195 | if (align < EFI_PAGE_SIZE) |
| 196 | align = EFI_PAGE_SIZE; |
| 197 | |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 198 | nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; |
| 199 | for (i = 0; i < map_size / desc_size; i++) { |
| 200 | efi_memory_desc_t *desc; |
| 201 | unsigned long m = (unsigned long)map; |
| 202 | u64 start, end; |
| 203 | |
| 204 | desc = (efi_memory_desc_t *)(m + (i * desc_size)); |
| 205 | |
| 206 | if (desc->type != EFI_CONVENTIONAL_MEMORY) |
| 207 | continue; |
| 208 | |
| 209 | if (desc->num_pages < nr_pages) |
| 210 | continue; |
| 211 | |
| 212 | start = desc->phys_addr; |
| 213 | end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT); |
| 214 | |
| 215 | /* |
| 216 | * Don't allocate at 0x0. It will confuse code that |
| 217 | * checks pointers against NULL. Skip the first 8 |
| 218 | * bytes so we start at a nice even number. |
| 219 | */ |
| 220 | if (start == 0x0) |
| 221 | start += 8; |
| 222 | |
| 223 | start = round_up(start, align); |
| 224 | if ((start + size) > end) |
| 225 | continue; |
| 226 | |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 227 | status = efi_call_phys4(sys_table_arg->boottime->allocate_pages, |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 228 | EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, |
| 229 | nr_pages, &start); |
| 230 | if (status == EFI_SUCCESS) { |
| 231 | *addr = start; |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if (i == map_size / desc_size) |
| 237 | status = EFI_NOT_FOUND; |
| 238 | |
| 239 | free_pool: |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 240 | efi_call_phys1(sys_table_arg->boottime->free_pool, map); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 241 | fail: |
| 242 | return status; |
| 243 | } |
| 244 | |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 245 | static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size, |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 246 | unsigned long addr) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 247 | { |
| 248 | unsigned long nr_pages; |
| 249 | |
| 250 | nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 251 | efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | |
| 255 | /* |
| 256 | * Check the cmdline for a LILO-style initrd= arguments. |
| 257 | * |
| 258 | * We only support loading an initrd from the same filesystem as the |
| 259 | * kernel image. |
| 260 | */ |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 261 | static efi_status_t handle_ramdisks(efi_system_table_t *sys_table_arg, |
| 262 | efi_loaded_image_t *image, |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 263 | struct setup_header *hdr) |
| 264 | { |
| 265 | struct initrd *initrds; |
| 266 | unsigned long initrd_addr; |
| 267 | efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; |
| 268 | u64 initrd_total; |
| 269 | efi_file_io_interface_t *io; |
| 270 | efi_file_handle_t *fh; |
| 271 | efi_status_t status; |
| 272 | int nr_initrds; |
| 273 | char *str; |
| 274 | int i, j, k; |
| 275 | |
| 276 | initrd_addr = 0; |
| 277 | initrd_total = 0; |
| 278 | |
| 279 | str = (char *)(unsigned long)hdr->cmd_line_ptr; |
| 280 | |
| 281 | j = 0; /* See close_handles */ |
| 282 | |
| 283 | if (!str || !*str) |
| 284 | return EFI_SUCCESS; |
| 285 | |
| 286 | for (nr_initrds = 0; *str; nr_initrds++) { |
| 287 | str = strstr(str, "initrd="); |
| 288 | if (!str) |
| 289 | break; |
| 290 | |
| 291 | str += 7; |
| 292 | |
| 293 | /* Skip any leading slashes */ |
| 294 | while (*str == '/' || *str == '\\') |
| 295 | str++; |
| 296 | |
| 297 | while (*str && *str != ' ' && *str != '\n') |
| 298 | str++; |
| 299 | } |
| 300 | |
| 301 | if (!nr_initrds) |
| 302 | return EFI_SUCCESS; |
| 303 | |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 304 | status = efi_call_phys3(sys_table_arg->boottime->allocate_pool, |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 305 | EFI_LOADER_DATA, |
| 306 | nr_initrds * sizeof(*initrds), |
| 307 | &initrds); |
| 308 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 309 | efi_printk(sys_table_arg, "Failed to alloc mem for initrds\n"); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 310 | goto fail; |
| 311 | } |
| 312 | |
| 313 | str = (char *)(unsigned long)hdr->cmd_line_ptr; |
| 314 | for (i = 0; i < nr_initrds; i++) { |
| 315 | struct initrd *initrd; |
| 316 | efi_file_handle_t *h; |
| 317 | efi_file_info_t *info; |
| 318 | efi_char16_t filename_16[256]; |
| 319 | unsigned long info_sz; |
| 320 | efi_guid_t info_guid = EFI_FILE_INFO_ID; |
| 321 | efi_char16_t *p; |
| 322 | u64 file_sz; |
| 323 | |
| 324 | str = strstr(str, "initrd="); |
| 325 | if (!str) |
| 326 | break; |
| 327 | |
| 328 | str += 7; |
| 329 | |
| 330 | initrd = &initrds[i]; |
| 331 | p = filename_16; |
| 332 | |
| 333 | /* Skip any leading slashes */ |
| 334 | while (*str == '/' || *str == '\\') |
| 335 | str++; |
| 336 | |
| 337 | while (*str && *str != ' ' && *str != '\n') { |
| 338 | if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16)) |
| 339 | break; |
| 340 | |
| 341 | if (*str == '/') { |
| 342 | *p++ = '\\'; |
| 343 | *str++; |
| 344 | } else { |
| 345 | *p++ = *str++; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | *p = '\0'; |
| 350 | |
| 351 | /* Only open the volume once. */ |
| 352 | if (!i) { |
| 353 | efi_boot_services_t *boottime; |
| 354 | |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 355 | boottime = sys_table_arg->boottime; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 356 | |
| 357 | status = efi_call_phys3(boottime->handle_protocol, |
| 358 | image->device_handle, &fs_proto, &io); |
| 359 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 360 | efi_printk(sys_table_arg, "Failed to handle fs_proto\n"); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 361 | goto free_initrds; |
| 362 | } |
| 363 | |
| 364 | status = efi_call_phys2(io->open_volume, io, &fh); |
| 365 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 366 | efi_printk(sys_table_arg, "Failed to open volume\n"); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 367 | goto free_initrds; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | status = efi_call_phys5(fh->open, fh, &h, filename_16, |
| 372 | EFI_FILE_MODE_READ, (u64)0); |
| 373 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 374 | efi_printk(sys_table_arg, "Failed to open initrd file: "); |
| 375 | efi_char16_printk(sys_table_arg, filename_16); |
| 376 | efi_printk(sys_table_arg, "\n"); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 377 | goto close_handles; |
| 378 | } |
| 379 | |
| 380 | initrd->handle = h; |
| 381 | |
| 382 | info_sz = 0; |
| 383 | status = efi_call_phys4(h->get_info, h, &info_guid, |
| 384 | &info_sz, NULL); |
| 385 | if (status != EFI_BUFFER_TOO_SMALL) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 386 | efi_printk(sys_table_arg, "Failed to get initrd info size\n"); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 387 | goto close_handles; |
| 388 | } |
| 389 | |
| 390 | grow: |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 391 | status = efi_call_phys3(sys_table_arg->boottime->allocate_pool, |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 392 | EFI_LOADER_DATA, info_sz, &info); |
| 393 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 394 | efi_printk(sys_table_arg, "Failed to alloc mem for initrd info\n"); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 395 | goto close_handles; |
| 396 | } |
| 397 | |
| 398 | status = efi_call_phys4(h->get_info, h, &info_guid, |
| 399 | &info_sz, info); |
| 400 | if (status == EFI_BUFFER_TOO_SMALL) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 401 | efi_call_phys1(sys_table_arg->boottime->free_pool, |
| 402 | info); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 403 | goto grow; |
| 404 | } |
| 405 | |
| 406 | file_sz = info->file_size; |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 407 | efi_call_phys1(sys_table_arg->boottime->free_pool, info); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 408 | |
| 409 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 410 | efi_printk(sys_table_arg, "Failed to get initrd info\n"); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 411 | goto close_handles; |
| 412 | } |
| 413 | |
| 414 | initrd->size = file_sz; |
| 415 | initrd_total += file_sz; |
| 416 | } |
| 417 | |
| 418 | if (initrd_total) { |
| 419 | unsigned long addr; |
| 420 | |
| 421 | /* |
| 422 | * Multiple initrd's need to be at consecutive |
| 423 | * addresses in memory, so allocate enough memory for |
| 424 | * all the initrd's. |
| 425 | */ |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 426 | status = efi_high_alloc(sys_table_arg, initrd_total, 0x1000, |
| 427 | &initrd_addr, hdr->initrd_addr_max); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 428 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 429 | efi_printk(sys_table_arg, "Failed to alloc highmem for initrds\n"); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 430 | goto close_handles; |
| 431 | } |
| 432 | |
| 433 | /* We've run out of free low memory. */ |
| 434 | if (initrd_addr > hdr->initrd_addr_max) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 435 | efi_printk(sys_table_arg, "We've run out of free low memory\n"); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 436 | status = EFI_INVALID_PARAMETER; |
| 437 | goto free_initrd_total; |
| 438 | } |
| 439 | |
| 440 | addr = initrd_addr; |
| 441 | for (j = 0; j < nr_initrds; j++) { |
| 442 | u64 size; |
| 443 | |
| 444 | size = initrds[j].size; |
| 445 | while (size) { |
| 446 | u64 chunksize; |
| 447 | if (size > EFI_READ_CHUNK_SIZE) |
| 448 | chunksize = EFI_READ_CHUNK_SIZE; |
| 449 | else |
| 450 | chunksize = size; |
| 451 | status = efi_call_phys3(fh->read, |
| 452 | initrds[j].handle, |
| 453 | &chunksize, addr); |
| 454 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 455 | efi_printk(sys_table_arg, "Failed to read initrd\n"); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 456 | goto free_initrd_total; |
| 457 | } |
| 458 | addr += chunksize; |
| 459 | size -= chunksize; |
| 460 | } |
| 461 | |
| 462 | efi_call_phys1(fh->close, initrds[j].handle); |
| 463 | } |
| 464 | |
| 465 | } |
| 466 | |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 467 | efi_call_phys1(sys_table_arg->boottime->free_pool, initrds); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 468 | |
| 469 | hdr->ramdisk_image = initrd_addr; |
| 470 | hdr->ramdisk_size = initrd_total; |
| 471 | |
| 472 | return status; |
| 473 | |
| 474 | free_initrd_total: |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 475 | efi_free(sys_table_arg, initrd_total, initrd_addr); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 476 | |
| 477 | close_handles: |
| 478 | for (k = j; k < i; k++) |
| 479 | efi_call_phys1(fh->close, initrds[k].handle); |
| 480 | free_initrds: |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 481 | efi_call_phys1(sys_table_arg->boottime->free_pool, initrds); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 482 | fail: |
| 483 | hdr->ramdisk_image = 0; |
| 484 | hdr->ramdisk_size = 0; |
| 485 | |
| 486 | return status; |
| 487 | } |