xref: /rk3399_rockchip-uboot/lib/efi_loader/efi_disk.c (revision 4a12a97c14217a5b02791040e464449f779fce43)
1 /*
2  *  EFI application disk support
3  *
4  *  Copyright (c) 2016 Alexander Graf
5  *
6  *  SPDX-License-Identifier:     GPL-2.0+
7  */
8 
9 #include <common.h>
10 #include <efi_loader.h>
11 #include <inttypes.h>
12 #include <part.h>
13 #include <malloc.h>
14 
15 static const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
16 
17 struct efi_disk_obj {
18 	/* Generic EFI object parent class data */
19 	struct efi_object parent;
20 	/* EFI Interface callback struct for block I/O */
21 	struct efi_block_io ops;
22 	/* U-Boot ifname for block device */
23 	const char *ifname;
24 	/* U-Boot dev_index for block device */
25 	int dev_index;
26 	/* EFI Interface Media descriptor struct, referenced by ops */
27 	struct efi_block_io_media media;
28 	/* EFI device path to this block device */
29 	struct efi_device_path_file_path *dp;
30 };
31 
32 static efi_status_t efi_disk_open_block(void *handle, efi_guid_t *protocol,
33 			void **protocol_interface, void *agent_handle,
34 			void *controller_handle, uint32_t attributes)
35 {
36 	struct efi_disk_obj *diskobj = handle;
37 
38 	*protocol_interface = &diskobj->ops;
39 
40 	return EFI_SUCCESS;
41 }
42 
43 static efi_status_t efi_disk_open_dp(void *handle, efi_guid_t *protocol,
44 			void **protocol_interface, void *agent_handle,
45 			void *controller_handle, uint32_t attributes)
46 {
47 	struct efi_disk_obj *diskobj = handle;
48 
49 	*protocol_interface = diskobj->dp;
50 
51 	return EFI_SUCCESS;
52 }
53 
54 static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
55 			char extended_verification)
56 {
57 	EFI_ENTRY("%p, %x", this, extended_verification);
58 	return EFI_EXIT(EFI_DEVICE_ERROR);
59 }
60 
61 enum efi_disk_direction {
62 	EFI_DISK_READ,
63 	EFI_DISK_WRITE,
64 };
65 
66 static efi_status_t EFIAPI efi_disk_rw_blocks(struct efi_block_io *this,
67 			u32 media_id, u64 lba, unsigned long buffer_size,
68 			void *buffer, enum efi_disk_direction direction)
69 {
70 	struct efi_disk_obj *diskobj;
71 	struct blk_desc *desc;
72 	int blksz;
73 	int blocks;
74 	unsigned long n;
75 
76 	EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
77 		  buffer_size, buffer);
78 
79 	diskobj = container_of(this, struct efi_disk_obj, ops);
80 	if (!(desc = blk_get_dev(diskobj->ifname, diskobj->dev_index)))
81 		return EFI_EXIT(EFI_DEVICE_ERROR);
82 	blksz = desc->blksz;
83 	blocks = buffer_size / blksz;
84 
85 #ifdef DEBUG_EFI
86 	printf("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
87 	       __LINE__, blocks, lba, blksz, direction);
88 #endif
89 
90 	/* We only support full block access */
91 	if (buffer_size & (blksz - 1))
92 		return EFI_EXIT(EFI_DEVICE_ERROR);
93 
94 	if (direction == EFI_DISK_READ)
95 		n = desc->block_read(desc, lba, blocks, buffer);
96 	else
97 		n = desc->block_write(desc, lba, blocks, buffer);
98 
99 	/* We don't do interrupts, so check for timers cooperatively */
100 	efi_timer_check();
101 
102 #ifdef DEBUG_EFI
103 	printf("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
104 #endif
105 	if (n != blocks)
106 		return EFI_EXIT(EFI_DEVICE_ERROR);
107 
108 	return EFI_EXIT(EFI_SUCCESS);
109 }
110 
111 static efi_status_t efi_disk_read_blocks(struct efi_block_io *this,
112 			u32 media_id, u64 lba, unsigned long buffer_size,
113 			void *buffer)
114 {
115 	return efi_disk_rw_blocks(this, media_id, lba, buffer_size, buffer,
116 				  EFI_DISK_READ);
117 }
118 
119 static efi_status_t efi_disk_write_blocks(struct efi_block_io *this,
120 			u32 media_id, u64 lba, unsigned long buffer_size,
121 			void *buffer)
122 {
123 	return efi_disk_rw_blocks(this, media_id, lba, buffer_size, buffer,
124 				  EFI_DISK_WRITE);
125 }
126 
127 static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
128 {
129 	/* We always write synchronously */
130 	EFI_ENTRY("%p", this);
131 	return EFI_EXIT(EFI_SUCCESS);
132 }
133 
134 static const struct efi_block_io block_io_disk_template = {
135 	.reset = &efi_disk_reset,
136 	.read_blocks = &efi_disk_read_blocks,
137 	.write_blocks = &efi_disk_write_blocks,
138 	.flush_blocks = &efi_disk_flush_blocks,
139 };
140 
141 static void efi_disk_add_dev(char *name,
142 			     const struct block_drvr *cur_drvr,
143 			     const struct blk_desc *desc,
144 			     int dev_index,
145 			     lbaint_t offset)
146 {
147 	struct efi_disk_obj *diskobj;
148 	struct efi_device_path_file_path *dp;
149 	int objlen = sizeof(*diskobj) + (sizeof(*dp) * 2);
150 
151 	diskobj = calloc(1, objlen);
152 
153 	/* Fill in object data */
154 	diskobj->parent.protocols[0].guid = &efi_block_io_guid;
155 	diskobj->parent.protocols[0].open = efi_disk_open_block;
156 	diskobj->parent.protocols[1].guid = &efi_guid_device_path;
157 	diskobj->parent.protocols[1].open = efi_disk_open_dp;
158 	diskobj->parent.handle = diskobj;
159 	diskobj->ops = block_io_disk_template;
160 	diskobj->ifname = cur_drvr->name;
161 	diskobj->dev_index = dev_index;
162 
163 	/* Fill in EFI IO Media info (for read/write callbacks) */
164 	diskobj->media.removable_media = desc->removable;
165 	diskobj->media.media_present = 1;
166 	diskobj->media.block_size = desc->blksz;
167 	diskobj->media.io_align = desc->blksz;
168 	diskobj->media.last_block = desc->lba;
169 	diskobj->ops.media = &diskobj->media;
170 
171 	/* Fill in device path */
172 	dp = (void*)&diskobj[1];
173 	diskobj->dp = dp;
174 	dp[0].dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
175 	dp[0].dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
176 	dp[0].dp.length = sizeof(*dp);
177 	ascii2unicode(dp[0].str, name);
178 
179 	dp[1].dp.type = DEVICE_PATH_TYPE_END;
180 	dp[1].dp.sub_type = DEVICE_PATH_SUB_TYPE_END;
181 	dp[1].dp.length = sizeof(*dp);
182 
183 	/* Hook up to the device list */
184 	list_add_tail(&diskobj->parent.link, &efi_obj_list);
185 }
186 
187 /*
188  * U-Boot doesn't have a list of all online disk devices. So when running our
189  * EFI payload, we scan through all of the potentially available ones and
190  * store them in our object pool.
191  *
192  * This gets called from do_bootefi_exec().
193  */
194 int efi_disk_register(void)
195 {
196 	const struct block_drvr *cur_drvr;
197 	int i;
198 	int disks = 0;
199 
200 	/* Search for all available disk devices */
201 	for (cur_drvr = block_drvr; cur_drvr->name; cur_drvr++) {
202 		printf("Scanning disks on %s...\n", cur_drvr->name);
203 		for (i = 0; i < 4; i++) {
204 			struct blk_desc *desc;
205 			char devname[16] = { 0 }; /* dp->str is u16[16] long */
206 
207 			desc = blk_get_dev(cur_drvr->name, i);
208 			if (!desc)
209 				continue;
210 			if (desc->type == DEV_TYPE_UNKNOWN)
211 				continue;
212 
213 			snprintf(devname, sizeof(devname), "%s%d",
214 				 cur_drvr->name, i);
215 			efi_disk_add_dev(devname, cur_drvr, desc, i, 0);
216 			disks++;
217 		}
218 	}
219 	printf("Found %d disks\n", disks);
220 
221 	return 0;
222 }
223