blob: ff69e6eb79b1332bc2988130e837a299c10a9999 [file] [log] [blame]
Ilias Apalodimas3510ba72020-02-21 09:55:45 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2020, Linaro Limited
4 */
5
6#include <common.h>
7#include <env.h>
8#include <malloc.h>
9#include <mapmem.h>
10#include <dm.h>
11#include <fs.h>
12#include <efi_loader.h>
13#include <efi_load_initrd.h>
14
15static const efi_guid_t efi_guid_load_file2_protocol =
16 EFI_LOAD_FILE2_PROTOCOL_GUID;
17
18static efi_status_t EFIAPI
19efi_load_file2_initrd(struct efi_load_file_protocol *this,
20 struct efi_device_path *file_path, bool boot_policy,
21 efi_uintn_t *buffer_size, void *buffer);
22
23static const struct efi_load_file_protocol efi_lf2_protocol = {
24 .load_file = efi_load_file2_initrd,
25};
26
27/*
28 * Device path defined by Linux to identify the handle providing the
29 * EFI_LOAD_FILE2_PROTOCOL used for loading the initial ramdisk.
30 */
31static const struct efi_initrd_dp dp = {
32 .vendor = {
33 {
34 DEVICE_PATH_TYPE_MEDIA_DEVICE,
35 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
36 sizeof(dp.vendor),
37 },
38 EFI_INITRD_MEDIA_GUID,
39 },
40 .end = {
41 DEVICE_PATH_TYPE_END,
42 DEVICE_PATH_SUB_TYPE_END,
43 sizeof(dp.end),
44 }
45};
46
47/**
48 * get_file_size() - retrieve the size of initramfs, set efi status on error
49 *
Heinrich Schuchardtd1d89332020-10-03 12:44:31 +020050 * @dev: device to read from, e.g. "mmc"
51 * @part: device partition, e.g. "0:1"
52 * @file: name of file
Ilias Apalodimas3510ba72020-02-21 09:55:45 +020053 * @status: EFI exit code in case of failure
54 *
55 * Return: size of file
56 */
57static loff_t get_file_size(const char *dev, const char *part, const char *file,
58 efi_status_t *status)
59{
60 loff_t sz = 0;
61 int ret;
62
63 ret = fs_set_blk_dev(dev, part, FS_TYPE_ANY);
64 if (ret) {
65 *status = EFI_NO_MEDIA;
66 goto out;
67 }
68
69 ret = fs_size(file, &sz);
70 if (ret) {
71 sz = 0;
72 *status = EFI_NOT_FOUND;
73 goto out;
74 }
75
76out:
77 return sz;
78}
79
80/**
Heinrich Schuchardtd1d89332020-10-03 12:44:31 +020081 * efi_load_file2initrd() - load initial RAM disk
Ilias Apalodimas3510ba72020-02-21 09:55:45 +020082 *
Heinrich Schuchardtd1d89332020-10-03 12:44:31 +020083 * This function implements the LoadFile service of the EFI_LOAD_FILE2_PROTOCOL
84 * in order to load an initial RAM disk requested by the Linux kernel stub.
85 *
Ilias Apalodimas3510ba72020-02-21 09:55:45 +020086 * See the UEFI spec for details.
87 *
Heinrich Schuchardtd1d89332020-10-03 12:44:31 +020088 * @this: EFI_LOAD_FILE2_PROTOCOL instance
89 * @file_path: media device path of the file, "" in this case
90 * @boot_policy: must be false
Ilias Apalodimas3510ba72020-02-21 09:55:45 +020091 * @buffer_size: size of allocated buffer
92 * @buffer: buffer to load the file
93 *
94 * Return: status code
95 */
96static efi_status_t EFIAPI
97efi_load_file2_initrd(struct efi_load_file_protocol *this,
98 struct efi_device_path *file_path, bool boot_policy,
99 efi_uintn_t *buffer_size, void *buffer)
100{
101 const char *filespec = CONFIG_EFI_INITRD_FILESPEC;
102 efi_status_t status = EFI_NOT_FOUND;
103 loff_t file_sz = 0, read_sz = 0;
104 char *dev, *part, *file;
105 char *s;
106 int ret;
107
108 EFI_ENTRY("%p, %p, %d, %p, %p", this, file_path, boot_policy,
109 buffer_size, buffer);
110
111 s = strdup(filespec);
112 if (!s)
113 goto out;
114
115 if (!this || this != &efi_lf2_protocol ||
116 !buffer_size) {
117 status = EFI_INVALID_PARAMETER;
118 goto out;
119 }
120
121 if (file_path->type != dp.end.type ||
122 file_path->sub_type != dp.end.sub_type) {
123 status = EFI_INVALID_PARAMETER;
124 goto out;
125 }
126
127 if (boot_policy) {
128 status = EFI_UNSUPPORTED;
129 goto out;
130 }
131
Heinrich Schuchardtd1d89332020-10-03 12:44:31 +0200132 /*
133 * expect a string with three space separated parts:
134 *
135 * * a block device type, e.g. "mmc"
136 * * a device and partition identifier, e.g. "0:1"
137 * * a file path on the block device, e.g. "/boot/initrd.cpio.gz"
138 */
Ilias Apalodimas3510ba72020-02-21 09:55:45 +0200139 dev = strsep(&s, " ");
140 if (!dev)
141 goto out;
142 part = strsep(&s, " ");
143 if (!part)
144 goto out;
145 file = strsep(&s, " ");
146 if (!file)
147 goto out;
148
149 file_sz = get_file_size(dev, part, file, &status);
150 if (!file_sz)
151 goto out;
152
153 if (!buffer || *buffer_size < file_sz) {
154 status = EFI_BUFFER_TOO_SMALL;
155 *buffer_size = file_sz;
156 } else {
157 ret = fs_set_blk_dev(dev, part, FS_TYPE_ANY);
158 if (ret) {
159 status = EFI_NO_MEDIA;
160 goto out;
161 }
162
163 ret = fs_read(file, map_to_sysmem(buffer), 0, *buffer_size,
164 &read_sz);
165 if (ret || read_sz != file_sz)
166 goto out;
167 *buffer_size = read_sz;
168
169 status = EFI_SUCCESS;
170 }
171
172out:
173 free(s);
174 return EFI_EXIT(status);
175}
176
177/**
Heinrich Schuchardtd1d89332020-10-03 12:44:31 +0200178 * efi_initrd_register() - create handle for loading initial RAM disk
Ilias Apalodimas3510ba72020-02-21 09:55:45 +0200179 *
Heinrich Schuchardtd1d89332020-10-03 12:44:31 +0200180 * This function creates a new handle and installs a Linux specific vendor
181 * device path and an EFI_LOAD_FILE_2_PROTOCOL. Linux uses the device path
182 * to identify the handle and then calls the LoadFile service of the
183 * EFI_LOAD_FILE_2_PROTOCOL to read the initial RAM disk.
Ilias Apalodimas3510ba72020-02-21 09:55:45 +0200184 *
Heinrich Schuchardtd1d89332020-10-03 12:44:31 +0200185 * Return: status code
Ilias Apalodimas3510ba72020-02-21 09:55:45 +0200186 */
187efi_status_t efi_initrd_register(void)
188{
189 efi_handle_t efi_initrd_handle = NULL;
190 efi_status_t ret;
191
Ilias Apalodimas3510ba72020-02-21 09:55:45 +0200192 ret = EFI_CALL(efi_install_multiple_protocol_interfaces
193 (&efi_initrd_handle,
194 /* initramfs */
195 &efi_guid_device_path, &dp,
196 /* LOAD_FILE2 */
197 &efi_guid_load_file2_protocol,
198 (void *)&efi_lf2_protocol,
199 NULL));
200
201 return ret;
202}