xref: /titanic_52/usr/src/boot/sys/boot/efi/boot1/ufs_module.c (revision 7ee590ef2b77e6a6869db9afcfa46e4950e11128)
1 /*-
2  * Copyright (c) 1998 Robert Nordier
3  * All rights reserved.
4  * Copyright (c) 2001 Robert Drehmel
5  * All rights reserved.
6  * Copyright (c) 2014 Nathan Whitehorn
7  * All rights reserved.
8  * Copyright (c) 2015 Eric McCorkle
9  * All rights reverved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <stdarg.h>
34 #include <stdbool.h>
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <efi.h>
38 
39 #include "boot_module.h"
40 
41 static dev_info_t *devinfo;
42 static dev_info_t *devices;
43 
44 static int
45 dskread(void *buf, u_int64_t lba, int nblk)
46 {
47 	int size;
48 	EFI_STATUS status;
49 
50 	lba = lba / (devinfo->dev->Media->BlockSize / DEV_BSIZE);
51 	size = nblk * DEV_BSIZE;
52 
53 	status = devinfo->dev->ReadBlocks(devinfo->dev,
54 	    devinfo->dev->Media->MediaId, lba, size, buf);
55 
56 	if (status != EFI_SUCCESS) {
57 		DPRINTF("dskread: failed dev: %p, id: %u, lba: %ju, size: %d, "
58 		    "status: %lu\n", devinfo->dev,
59 		    devinfo->dev->Media->MediaId, (uintmax_t)lba, size,
60 		    EFI_ERROR_CODE(status));
61 		return (-1);
62 	}
63 
64 	return (0);
65 }
66 
67 #include "ufsread.c"
68 
69 static struct dmadat __dmadat;
70 
71 static int
72 init_dev(dev_info_t* dev)
73 {
74 
75 	devinfo = dev;
76 	dmadat = &__dmadat;
77 
78 	return fsread(0, NULL, 0);
79 }
80 
81 static EFI_STATUS
82 probe(dev_info_t* dev)
83 {
84 
85 	if (init_dev(dev) < 0)
86 		return (EFI_UNSUPPORTED);
87 
88 	add_device(&devices, dev);
89 
90 	return (EFI_SUCCESS);
91 }
92 
93 static EFI_STATUS
94 load(const char *filepath, dev_info_t *dev, void **bufp, size_t *bufsize)
95 {
96 	ufs_ino_t ino;
97 	EFI_STATUS status;
98 	size_t size;
99 	ssize_t read;
100 	void *buf;
101 
102 	DPRINTF("Loading '%s' from %s\n", filepath, devpath_str(dev->devpath));
103 
104 	if (init_dev(dev) < 0) {
105 		DPRINTF("Failed to init device\n");
106 		return (EFI_UNSUPPORTED);
107 	}
108 
109 	if ((ino = lookup(filepath)) == 0) {
110 		DPRINTF("Failed to lookup '%s' (file not found?)\n", filepath);
111 		return (EFI_NOT_FOUND);
112 	}
113 
114 	if (fsread_size(ino, NULL, 0, &size) < 0 || size <= 0) {
115 		printf("Failed to read size of '%s' ino: %d\n", filepath, ino);
116 		return (EFI_INVALID_PARAMETER);
117 	}
118 
119 	if ((status = bs->AllocatePool(EfiLoaderData, size, &buf)) !=
120 	    EFI_SUCCESS) {
121 		printf("Failed to allocate read buffer %zu for '%s' (%lu)\n",
122 		    size, filepath, EFI_ERROR_CODE(status));
123 		return (status);
124 	}
125 
126 	read = fsread(ino, buf, size);
127 	if ((size_t)read != size) {
128 		printf("Failed to read '%s' (%zd != %zu)\n", filepath, read,
129 		    size);
130 		(void)bs->FreePool(buf);
131 		return (EFI_INVALID_PARAMETER);
132 	}
133 
134 	DPRINTF("Load complete\n");
135 
136 	*bufp = buf;
137 	*bufsize = size;
138 
139 	return (EFI_SUCCESS);
140 }
141 
142 static void
143 status(void)
144 {
145 	int i;
146 	dev_info_t *dev;
147 
148 	for (dev = devices, i = 0; dev != NULL; dev = dev->next, i++)
149 		;
150 
151 	printf("%s found ", ufs_module.name);
152 	switch (i) {
153 	case 0:
154 		printf("no partitions\n");
155 		break;
156 	case 1:
157 		printf("%d partition\n", i);
158 		break;
159 	default:
160 		printf("%d partitions\n", i);
161 	}
162 }
163 
164 static dev_info_t *
165 _devices(void)
166 {
167 
168 	return (devices);
169 }
170 
171 const boot_module_t ufs_module =
172 {
173 	.name = "UFS",
174 	.probe = probe,
175 	.load = load,
176 	.status = status,
177 	.devices = _devices
178 };
179