xref: /freebsd/stand/efi/boot1/ufs_module.c (revision 7c43148a974877188a930e4078a164f83da8e652)
1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh  * Copyright (c) 1998 Robert Nordier
3ca987d46SWarner Losh  * All rights reserved.
4ca987d46SWarner Losh  * Copyright (c) 2001 Robert Drehmel
5ca987d46SWarner Losh  * All rights reserved.
6ca987d46SWarner Losh  * Copyright (c) 2014 Nathan Whitehorn
7ca987d46SWarner Losh  * All rights reserved.
8ca987d46SWarner Losh  * Copyright (c) 2015 Eric McCorkle
9ca987d46SWarner Losh  * All rights reverved.
10ca987d46SWarner Losh  *
11ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
12ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
13ca987d46SWarner Losh  * are met:
14ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
15ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
16ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
17ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
18ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
19ca987d46SWarner Losh  *
20ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30ca987d46SWarner Losh  * SUCH DAMAGE.
31ca987d46SWarner Losh  */
32ca987d46SWarner Losh 
33ca987d46SWarner Losh #include <stdarg.h>
34ca987d46SWarner Losh #include <stdbool.h>
35*7c43148aSWarner Losh 
36ca987d46SWarner Losh #include <sys/param.h>
3799589326SWarner Losh #include <sys/disk/bsd.h>
38ca987d46SWarner Losh #include <efi.h>
39ca987d46SWarner Losh 
40ca987d46SWarner Losh #include "boot_module.h"
41ca987d46SWarner Losh 
4299589326SWarner Losh #define BSD_LABEL_BUFFER	8192
4399589326SWarner Losh #define BSD_LABEL_OFFSET	DEV_BSIZE
4499589326SWarner Losh 
45ca987d46SWarner Losh static dev_info_t *devinfo;
46ca987d46SWarner Losh static dev_info_t *devices;
47ca987d46SWarner Losh 
48ca987d46SWarner Losh static int
dskread(void * buf,uint64_t lba,int nblk)4956e53cb8SWarner Losh dskread(void *buf, uint64_t lba, int nblk)
50ca987d46SWarner Losh {
51ca987d46SWarner Losh 	int size;
52ca987d46SWarner Losh 	EFI_STATUS status;
53ca987d46SWarner Losh 
5499589326SWarner Losh 	lba += devinfo->partoff;
55ca987d46SWarner Losh 	lba = lba / (devinfo->dev->Media->BlockSize / DEV_BSIZE);
56ca987d46SWarner Losh 	size = nblk * DEV_BSIZE;
57ca987d46SWarner Losh 
58ca987d46SWarner Losh 	status = devinfo->dev->ReadBlocks(devinfo->dev,
59ca987d46SWarner Losh 	    devinfo->dev->Media->MediaId, lba, size, buf);
60ca987d46SWarner Losh 
61ca987d46SWarner Losh 	if (status != EFI_SUCCESS) {
62ca987d46SWarner Losh 		DPRINTF("dskread: failed dev: %p, id: %u, lba: %ju, size: %d, "
63ca987d46SWarner Losh 		    "status: %lu\n", devinfo->dev,
64ca987d46SWarner Losh 		    devinfo->dev->Media->MediaId, (uintmax_t)lba, size,
65ca987d46SWarner Losh 		    EFI_ERROR_CODE(status));
66ca987d46SWarner Losh 		return (-1);
67ca987d46SWarner Losh 	}
68ca987d46SWarner Losh 
69ca987d46SWarner Losh 	return (0);
70ca987d46SWarner Losh }
71ca987d46SWarner Losh 
72ca987d46SWarner Losh #include "ufsread.c"
73ca987d46SWarner Losh 
743d5e12ebSAndrew Turner static struct dmadat __dmadat __aligned(512);
753d5e12ebSAndrew Turner static char ufs_buffer[BSD_LABEL_BUFFER] __aligned(512);
76ca987d46SWarner Losh 
77ca987d46SWarner Losh static int
init_dev(dev_info_t * dev)78ca987d46SWarner Losh init_dev(dev_info_t* dev)
79ca987d46SWarner Losh {
8099589326SWarner Losh 	struct disklabel *dl;
8199589326SWarner Losh 	uint64_t bs;
8299589326SWarner Losh 	int ok;
83ca987d46SWarner Losh 
84ca987d46SWarner Losh 	devinfo = dev;
85ca987d46SWarner Losh 	dmadat = &__dmadat;
86ca987d46SWarner Losh 
8799589326SWarner Losh 	/*
8899589326SWarner Losh 	 * First try offset 0. This is the typical GPT case where we have no
8999589326SWarner Losh 	 * further partitioning (as well as the degenerate MBR case where
9099589326SWarner Losh 	 * the bsdlabel has a 0 offset).
9199589326SWarner Losh 	 */
9299589326SWarner Losh 	devinfo->partoff = 0;
9399589326SWarner Losh 	ok = fsread(0, NULL, 0);
9499589326SWarner Losh 	if (ok >= 0)
9599589326SWarner Losh 		return (ok);
9699589326SWarner Losh 
9799589326SWarner Losh 	/*
9899589326SWarner Losh 	 * Next, we look for a bsdlabel. This is technically located in sector
9999589326SWarner Losh 	 * 1. For 4k sectors, this offset is 4096, for 512b sectors it's
10099589326SWarner Losh 	 * 512. However, we have to fall back to 512 here because we create
10199589326SWarner Losh 	 * images that assume 512 byte blocks, but these can be put on devices
10299589326SWarner Losh 	 * who have 4k (or other) block sizes. If there's a crazy block size, we
10399589326SWarner Losh 	 * skip the 'at one sector' and go stright to checking at 512 bytes.
10499589326SWarner Losh 	 * There are other offsets that are historic, but we don't probe those
10599589326SWarner Losh 	 * since they were never used for MBR disks on FreeBSD on systems that
10699589326SWarner Losh 	 * could boot UEFI. UEFI is little endian only, as are BSD labels. We
10799589326SWarner Losh 	 * will retry fsread(0) only if there's a label found with a non-zero
10899589326SWarner Losh 	 * offset.
10999589326SWarner Losh 	 */
1103d5e12ebSAndrew Turner 	if (dskread(ufs_buffer, 0, BSD_LABEL_BUFFER / DEV_BSIZE) != 0)
11199589326SWarner Losh 		return (-1);
11299589326SWarner Losh 	dl = NULL;
11399589326SWarner Losh 	bs = devinfo->dev->Media->BlockSize;
11499589326SWarner Losh 	if (bs != 0 && bs <= BSD_LABEL_BUFFER / 2)
1153d5e12ebSAndrew Turner 		dl = (struct disklabel *)&ufs_buffer[bs];
11699589326SWarner Losh 	if (dl == NULL || dl->d_magic != BSD_MAGIC || dl->d_magic2 != BSD_MAGIC)
1173d5e12ebSAndrew Turner 		dl = (struct disklabel *)&ufs_buffer[BSD_LABEL_OFFSET];
11899589326SWarner Losh 	if (dl->d_magic != BSD_MAGIC || dl->d_magic2 != BSD_MAGIC ||
11999589326SWarner Losh 	    dl->d_partitions[0].p_offset == 0)
12099589326SWarner Losh 		return (-1);
12199589326SWarner Losh 	devinfo->partoff = dl->d_partitions[0].p_offset;
12299589326SWarner Losh 	return (fsread(0, NULL, 0));
123ca987d46SWarner Losh }
124ca987d46SWarner Losh 
125ca987d46SWarner Losh static EFI_STATUS
probe(dev_info_t * dev)126ca987d46SWarner Losh probe(dev_info_t* dev)
127ca987d46SWarner Losh {
128ca987d46SWarner Losh 
129ca987d46SWarner Losh 	if (init_dev(dev) < 0)
130ca987d46SWarner Losh 		return (EFI_UNSUPPORTED);
131ca987d46SWarner Losh 
132ca987d46SWarner Losh 	add_device(&devices, dev);
133ca987d46SWarner Losh 
134ca987d46SWarner Losh 	return (EFI_SUCCESS);
135ca987d46SWarner Losh }
136ca987d46SWarner Losh 
137ca987d46SWarner Losh static EFI_STATUS
load(const char * filepath,dev_info_t * dev,void ** bufp,size_t * bufsize)138ca987d46SWarner Losh load(const char *filepath, dev_info_t *dev, void **bufp, size_t *bufsize)
139ca987d46SWarner Losh {
140ca987d46SWarner Losh 	ufs_ino_t ino;
141ca987d46SWarner Losh 	size_t size;
142ca987d46SWarner Losh 	ssize_t read;
143ca987d46SWarner Losh 	void *buf;
144ca987d46SWarner Losh 
145ca987d46SWarner Losh #ifdef EFI_DEBUG
146ca987d46SWarner Losh 	{
147ca987d46SWarner Losh 		CHAR16 *text = efi_devpath_name(dev->devpath);
148375cd3bbSWarner Losh 		DPRINTF("UFS Loading '%s' from %S\n", filepath, text);
149ca987d46SWarner Losh 		efi_free_devpath_name(text);
150ca987d46SWarner Losh 	}
151ca987d46SWarner Losh #endif
152ca987d46SWarner Losh 	if (init_dev(dev) < 0) {
153ca987d46SWarner Losh 		DPRINTF("Failed to init device\n");
154ca987d46SWarner Losh 		return (EFI_UNSUPPORTED);
155ca987d46SWarner Losh 	}
156ca987d46SWarner Losh 
157ca987d46SWarner Losh 	if ((ino = lookup(filepath)) == 0) {
158ca987d46SWarner Losh 		DPRINTF("Failed to lookup '%s' (file not found?)\n", filepath);
159ca987d46SWarner Losh 		return (EFI_NOT_FOUND);
160ca987d46SWarner Losh 	}
161ca987d46SWarner Losh 
162ca987d46SWarner Losh 	if (fsread_size(ino, NULL, 0, &size) < 0 || size <= 0) {
163ca987d46SWarner Losh 		printf("Failed to read size of '%s' ino: %d\n", filepath, ino);
164ca987d46SWarner Losh 		return (EFI_INVALID_PARAMETER);
165ca987d46SWarner Losh 	}
166ca987d46SWarner Losh 
167fef7bfefSWarner Losh 	buf = malloc(size);
168fef7bfefSWarner Losh 	if (buf == NULL) {
169fef7bfefSWarner Losh 		printf("Failed to allocate read buffer %zu for '%s'\n",
170fef7bfefSWarner Losh 		    size, filepath);
171fef7bfefSWarner Losh 		return (EFI_OUT_OF_RESOURCES);
172ca987d46SWarner Losh 	}
173ca987d46SWarner Losh 
174ca987d46SWarner Losh 	read = fsread(ino, buf, size);
175ca987d46SWarner Losh 	if ((size_t)read != size) {
176ca987d46SWarner Losh 		printf("Failed to read '%s' (%zd != %zu)\n", filepath, read,
177ca987d46SWarner Losh 		    size);
178fef7bfefSWarner Losh 		free(buf);
179ca987d46SWarner Losh 		return (EFI_INVALID_PARAMETER);
180ca987d46SWarner Losh 	}
181ca987d46SWarner Losh 
182ca987d46SWarner Losh 	DPRINTF("Load complete\n");
183ca987d46SWarner Losh 
184ca987d46SWarner Losh 	*bufp = buf;
185ca987d46SWarner Losh 	*bufsize = size;
186ca987d46SWarner Losh 
187ca987d46SWarner Losh 	return (EFI_SUCCESS);
188ca987d46SWarner Losh }
189ca987d46SWarner Losh 
190ca987d46SWarner Losh static void
status(void)191ca987d46SWarner Losh status(void)
192ca987d46SWarner Losh {
193ca987d46SWarner Losh 	int i;
194ca987d46SWarner Losh 	dev_info_t *dev;
195ca987d46SWarner Losh 
196ca987d46SWarner Losh 	for (dev = devices, i = 0; dev != NULL; dev = dev->next, i++)
197ca987d46SWarner Losh 		;
198ca987d46SWarner Losh 
199ca987d46SWarner Losh 	printf("%s found ", ufs_module.name);
200ca987d46SWarner Losh 	switch (i) {
201ca987d46SWarner Losh 	case 0:
202ca987d46SWarner Losh 		printf("no partitions\n");
203ca987d46SWarner Losh 		break;
204ca987d46SWarner Losh 	case 1:
205ca987d46SWarner Losh 		printf("%d partition\n", i);
206ca987d46SWarner Losh 		break;
207ca987d46SWarner Losh 	default:
208ca987d46SWarner Losh 		printf("%d partitions\n", i);
209ca987d46SWarner Losh 	}
210ca987d46SWarner Losh }
211ca987d46SWarner Losh 
212ca987d46SWarner Losh static dev_info_t *
_devices(void)213ca987d46SWarner Losh _devices(void)
214ca987d46SWarner Losh {
215ca987d46SWarner Losh 
216ca987d46SWarner Losh 	return (devices);
217ca987d46SWarner Losh }
218ca987d46SWarner Losh 
219ca987d46SWarner Losh const boot_module_t ufs_module =
220ca987d46SWarner Losh {
221ca987d46SWarner Losh 	.name = "UFS",
222ca987d46SWarner Losh 	.probe = probe,
223ca987d46SWarner Losh 	.load = load,
224ca987d46SWarner Losh 	.status = status,
225ca987d46SWarner Losh 	.devices = _devices
226ca987d46SWarner Losh };
227