sys/boot/efi/boot1: Port efi boot1 loader from FreeBSD to DragonFly.
[dragonfly.git] / sys / boot / efi / boot1 / ufs_module.c
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  * $FreeBSD: head/sys/boot/efi/boot1/ufs_module.c 295320 2016-02-05 15:35:33Z smh $
33  */
34
35 #include <stdarg.h>
36 #include <stdbool.h>
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <efi.h>
40
41 #include "boot_module.h"
42
43 static dev_info_t *devinfo;
44 static dev_info_t *devices;
45
46 static int
47 dskread(void *buf, u_int64_t lba, int nblk)
48 {
49         int size;
50         EFI_STATUS status;
51
52         lba = lba / (devinfo->dev->Media->BlockSize / DEV_BSIZE);
53         size = nblk * DEV_BSIZE;
54
55         status = devinfo->dev->ReadBlocks(devinfo->dev,
56             devinfo->dev->Media->MediaId, lba, size, buf);
57
58         if (status != EFI_SUCCESS) {
59                 DPRINTF("dskread: failed dev: %p, id: %u, lba: %lu, size: %d, "
60                     "status: %lu\n", devinfo->dev,
61                     devinfo->dev->Media->MediaId, lba, size,
62                     EFI_ERROR_CODE(status));
63                 return (-1);
64         }
65
66         return (0);
67 }
68
69 /* For ufsread.c */
70 uint64_t fs_off;
71 int ls;
72
73 static struct ufs_dmadat __dmadat;
74 static struct ufs_dmadat *boot2_dmadat;
75
76 #include "ufsread.c"
77
78 #define fsread boot2_ufs_read
79 #define fsread_size boot2_ufs_read_size
80 #define lookup boot2_ufs_lookup
81
82 static int
83 init_dev(dev_info_t* dev)
84 {
85
86         devinfo = dev;
87         boot2_dmadat = &__dmadat;
88
89         return fsread(0, NULL, 0);
90 }
91
92 static EFI_STATUS
93 probe(dev_info_t* dev)
94 {
95
96         if (init_dev(dev) < 0)
97                 return (EFI_UNSUPPORTED);
98
99         add_device(&devices, dev);
100
101         return (EFI_SUCCESS);
102 }
103
104 static EFI_STATUS
105 load(const char *filepath, dev_info_t *dev, void **bufp, size_t *bufsize)
106 {
107         ufs_ino_t ino;
108         EFI_STATUS status;
109         size_t size;
110         ssize_t read;
111         void *buf;
112
113         DPRINTF("Loading '%s' from %s\n", filepath, devpath_str(dev->devpath));
114
115         if (init_dev(dev) < 0) {
116                 DPRINTF("Failed to init device\n");
117                 return (EFI_UNSUPPORTED);
118         }
119
120         if ((ino = lookup(filepath)) == 0) {
121                 DPRINTF("Failed to lookup '%s' (file not found?)\n", filepath);
122                 return (EFI_NOT_FOUND);
123         }
124
125         if (fsread_size(ino, NULL, 0, &size) < 0 || size <= 0) {
126                 printf("Failed to read size of '%s' ino: %d\n", filepath, ino);
127                 return (EFI_INVALID_PARAMETER);
128         }
129
130         if ((status = bs->AllocatePool(EfiLoaderData, size, &buf)) !=
131             EFI_SUCCESS) {
132                 printf("Failed to allocate read buffer %zu for '%s' (%lu)\n",
133                     size, filepath, EFI_ERROR_CODE(status));
134                 return (status);
135         }
136
137         read = fsread(ino, buf, size);
138         if ((size_t)read != size) {
139                 printf("Failed to read '%s' (%zd != %zu)\n", filepath, read,
140                     size);
141                 (void)bs->FreePool(buf);
142                 return (EFI_INVALID_PARAMETER);
143         }
144
145         DPRINTF("Load complete\n");
146
147         *bufp = buf;
148         *bufsize = size;
149
150         return (EFI_SUCCESS);
151 }
152
153 static void
154 status(void)
155 {
156         int i;
157         dev_info_t *dev;
158
159         for (dev = devices, i = 0; dev != NULL; dev = dev->next, i++)
160                 ;
161
162         printf("%s found ", ufs_module.name);
163         switch (i) {
164         case 0:
165                 printf("no partitions\n");
166                 break;
167         case 1:
168                 printf("%d partition\n", i);
169                 break;
170         default:
171                 printf("%d partitions\n", i);
172         }
173 }
174
175 static dev_info_t *
176 _devices(void)
177 {
178
179         return (devices);
180 }
181
182 const boot_module_t ufs_module =
183 {
184         .name = "UFS",
185         .probe = probe,
186         .load = load,
187         .status = status,
188         .devices = _devices
189 };