David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Scull | 9c251d3 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 17 | #include "hf/cpio.h" |
| 18 | #include "hf/dlog.h" |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 19 | #include "hf/fdt_handler.h" |
| 20 | #include "hf/plat/boot_flow.h" |
Andrew Scull | 9c251d3 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 21 | #include "hf/std.h" |
| 22 | |
| 23 | /** |
| 24 | * Looks for a file in the given cpio archive. The file, if found, is returned |
| 25 | * in the "it" argument. |
| 26 | */ |
| 27 | static bool find_file(const struct memiter *cpio, const char *name, |
| 28 | struct memiter *it) |
| 29 | { |
| 30 | const char *fname; |
| 31 | const void *fcontents; |
| 32 | size_t fsize; |
| 33 | struct memiter iter = *cpio; |
| 34 | |
| 35 | while (cpio_next(&iter, &fname, &fcontents, &fsize)) { |
| 36 | if (!strcmp(fname, name)) { |
| 37 | memiter_init(it, fcontents, fsize); |
| 38 | return true; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return false; |
| 43 | } |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 44 | |
| 45 | /* Set by arch-specific boot-time hook. */ |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 46 | uintreg_t plat_boot_flow_fdt_addr; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Returns the physical address of board FDT. This was passed to Hafnium in the |
| 50 | * first kernel arg by the boot loader. |
| 51 | */ |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 52 | paddr_t plat_boot_flow_get_fdt_addr(void) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 53 | { |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 54 | return pa_init((uintpaddr_t)plat_boot_flow_fdt_addr); |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
| 58 | * When handing over to the primary, give it the same FDT address that was given |
| 59 | * to Hafnium. The FDT may have been modified during Hafnium init. |
| 60 | */ |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 61 | uintreg_t plat_boot_flow_get_kernel_arg(void) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 62 | { |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 63 | return plat_boot_flow_fdt_addr; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Load initrd range from the board FDT. |
| 68 | */ |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 69 | bool plat_boot_flow_get_initrd_range(const struct fdt_node *fdt_root, |
| 70 | paddr_t *begin, paddr_t *end) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 71 | { |
| 72 | return fdt_find_initrd(fdt_root, begin, end); |
| 73 | } |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 74 | |
| 75 | bool plat_boot_flow_update(struct mm_stage1_locked stage1_locked, |
Andrew Scull | 9c251d3 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 76 | struct boot_params_update *update, |
| 77 | struct memiter *cpio, struct mpool *ppool) |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 78 | { |
Andrew Scull | 9c251d3 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 79 | struct memiter primary_initrd; |
| 80 | |
| 81 | if (!find_file(cpio, "initrd.img", &primary_initrd)) { |
| 82 | dlog("Unable to find initrd.img\n"); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | update->initrd_begin = pa_from_va(va_from_ptr(primary_initrd.next)); |
| 87 | update->initrd_end = pa_from_va(va_from_ptr(primary_initrd.limit)); |
| 88 | |
| 89 | return fdt_patch(stage1_locked, plat_boot_flow_get_fdt_addr(), update, |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 90 | ppool); |
| 91 | } |