1 /*
2 * Copyright (C) 2025 The Android Open Source Project
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 * http://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 */
17
18 #include "uefi_platform.h"
19
20 #include <arch/arm64.h>
21 #include <libfdt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <uefi/protocols/gbl_efi_image_loading_protocol.h>
25 #include <uefi/protocols/gbl_efi_os_configuration_protocol.h>
26 #include <uefi/types.h>
27
28 #include "defer.h"
29
efi_dt_fixup(struct EfiDtFixupProtocol * self,void * fdt,size_t * buffer_size,uint32_t flags)30 __WEAK EFI_STATUS efi_dt_fixup(struct EfiDtFixupProtocol *self, void *fdt,
31 size_t *buffer_size, uint32_t flags) {
32 auto offset = fdt_subnode_offset(fdt, 0, "chosen");
33 if (offset < 0) {
34 printf("Failed to find chosen node %d\n", offset);
35 return SUCCESS;
36 }
37 int length = 0;
38 auto prop = fdt_get_property(fdt, offset, "bootargs", &length);
39
40 if (prop == nullptr) {
41 printf("Failed to find chosen/bootargs prop\n");
42 return SUCCESS;
43 }
44 char *new_prop_data = reinterpret_cast<char *>(malloc(length));
45 DEFER {
46 free(new_prop_data);
47 new_prop_data = nullptr;
48 };
49 auto prop_length = strnlen(prop->data, length);
50 static constexpr auto &&to_add =
51 "console=ttyAMA0 earlycon=pl011,mmio32,0x9000000 ";
52 memset(new_prop_data, 0, length);
53 memcpy(new_prop_data, to_add, sizeof(to_add) - 1);
54 memcpy(new_prop_data + sizeof(to_add) - 1, prop->data, prop_length);
55 auto ret = fdt_setprop(fdt, offset, "bootargs", new_prop_data, length);
56
57 printf("chosen/bootargs: %d %d \"%s\"\n", ret, length, new_prop_data);
58
59 return SUCCESS;
60 }
61
62 // Generates fixups for the bootconfig built by GBL.
fixup_bootconfig(struct GblEfiOsConfigurationProtocol * self,const char * bootconfig,size_t size,char * fixup,size_t * fixup_buffer_size)63 __WEAK EfiStatus fixup_bootconfig(struct GblEfiOsConfigurationProtocol *self,
64 const char *bootconfig, size_t size,
65 char *fixup, size_t *fixup_buffer_size) {
66 printf("%s(%p, %s, %lu, %lu)\n", __FUNCTION__, self, bootconfig, size,
67 *fixup_buffer_size);
68 constexpr auto &&to_add =
69 "\nandroidboot.fstab_suffix=cf.f2fs."
70 "hctr2\nandroidboot.boot_devices=4010000000.pcie";
71 const auto final_len = sizeof(to_add);
72 if (final_len > *fixup_buffer_size) {
73 *fixup_buffer_size = final_len;
74 return OUT_OF_RESOURCES;
75 }
76 *fixup_buffer_size = final_len;
77 memcpy(fixup, to_add, final_len);
78
79 return SUCCESS;
80 }
81
82 // Selects which device trees and overlays to use from those loaded by GBL.
select_device_trees(struct GblEfiOsConfigurationProtocol * self,GblEfiVerifiedDeviceTree * device_trees,size_t num_device_trees)83 __WEAK EfiStatus select_device_trees(struct GblEfiOsConfigurationProtocol *self,
84 GblEfiVerifiedDeviceTree *device_trees,
85 size_t num_device_trees) {
86 printf("%s(%p, %p %lu)\n", __FUNCTION__, self, device_trees,
87 num_device_trees);
88 return UNSUPPORTED;
89 }
90
exit_boot_services(EfiHandle image_handle,size_t map_key)91 __WEAK EfiStatus exit_boot_services(EfiHandle image_handle, size_t map_key) {
92 printf("%s is called\n", __FUNCTION__);
93 return SUCCESS;
94 }
95
platform_setup_system_table(EfiSystemTable * table)96 __WEAK EfiStatus platform_setup_system_table(EfiSystemTable *table) {
97 printf("%s is called\n", __FUNCTION__);
98 return SUCCESS;
99 }
100
get_timestamp()101 __WEAK uint64_t get_timestamp() {
102 return ARM64_READ_SYSREG(cntpct_el0);
103 }
104
get_timestamp_properties(EfiTimestampProperties * properties)105 __WEAK EfiStatus get_timestamp_properties(EfiTimestampProperties *properties) {
106 if (properties == nullptr) {
107 return INVALID_PARAMETER;
108 }
109 properties->frequency = ARM64_READ_SYSREG(cntfrq_el0) & 0xFFFFFFFF;
110 properties->end_value = UINT64_MAX;
111 return SUCCESS;
112 }
113
114 namespace {
115
GetImageType(const char16_t * ImageType)116 const char *GetImageType(const char16_t *ImageType) {
117 if (memcmp(ImageType, GBL_IMAGE_TYPE_OS_LOAD,
118 sizeof(GBL_IMAGE_TYPE_OS_LOAD)) == 0) {
119 return "os_load";
120 } else if (memcmp(ImageType, GBL_IMAGE_TYPE_FASTBOOT,
121 sizeof(GBL_IMAGE_TYPE_FASTBOOT)) == 0) {
122 return "fastboot";
123 } else if (memcmp(ImageType, GBL_IMAGE_TYPE_PVMFW_DATA,
124 sizeof(GBL_IMAGE_TYPE_PVMFW_DATA)) == 0) {
125 return "pvmfw_data";
126 }
127 return "unknown";
128 }
clamp(T n,T lower,T upper)129 template <typename T> T clamp(T n, T lower, T upper) {
130 if (n < lower) {
131 return lower;
132 }
133 if (n > upper) {
134 return upper;
135 }
136 return n;
137 }
138
139 } // namespace
140
get_buffer(struct GblEfiImageLoadingProtocol * self,const GblEfiImageInfo * ImageInfo,GblEfiImageBuffer * Buffer)141 __WEAK EfiStatus get_buffer(struct GblEfiImageLoadingProtocol *self,
142 const GblEfiImageInfo *ImageInfo,
143 GblEfiImageBuffer *Buffer) {
144 printf("%s(%s, %lu)\n", __FUNCTION__, GetImageType(ImageInfo->ImageType),
145 ImageInfo->SizeBytes);
146
147 // Allow maximum of 128MB buffer
148 const size_t buffer_size =
149 clamp(Buffer->SizeBytes, PAGE_SIZE, 128ul * 1024 * 1024);
150 // Bottom line, kernel, ramdisk, device tree must be identity mapped.
151 // Otherwise linux kernel would crash immediately after entering.
152 // Other buffers can be allocated from kernel address space, up to
153 // OEM for customization.
154 Buffer->Memory = alloc_page(buffer_size);
155 if (Buffer->Memory == nullptr) {
156 return OUT_OF_RESOURCES;
157 }
158
159 Buffer->SizeBytes = buffer_size;
160 return SUCCESS;
161 }
162