]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/fs/fshelp.c
Add mouse support for uefi (#1457)
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / grub-core / fs / fshelp.c
1 /* fshelp.c -- Filesystem helper functions */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2004,2005,2006,2007,2008 Free Software Foundation, Inc.
5 *
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <grub/err.h>
21 #include <grub/mm.h>
22 #include <grub/misc.h>
23 #include <grub/disk.h>
24 #include <grub/fshelp.h>
25 #include <grub/dl.h>
26 #include <grub/i18n.h>
27
28 GRUB_MOD_LICENSE ("GPLv3+");
29
30 typedef int (*iterate_dir_func) (grub_fshelp_node_t dir,
31 grub_fshelp_iterate_dir_hook_t hook,
32 void *data);
33 typedef grub_err_t (*lookup_file_func) (grub_fshelp_node_t dir,
34 const char *name,
35 grub_fshelp_node_t *foundnode,
36 enum grub_fshelp_filetype *foundtype);
37 typedef char *(*read_symlink_func) (grub_fshelp_node_t node);
38
39 struct stack_element {
40 struct stack_element *parent;
41 grub_fshelp_node_t node;
42 enum grub_fshelp_filetype type;
43 };
44
45 /* Context for grub_fshelp_find_file. */
46 struct grub_fshelp_find_file_ctx
47 {
48 /* Inputs. */
49 const char *path;
50 grub_fshelp_node_t rootnode;
51
52 /* Global options. */
53 int symlinknest;
54
55 /* Current file being traversed and its parents. */
56 struct stack_element *currnode;
57 };
58
59 /* Helper for find_file_iter. */
60 static void
61 free_node (grub_fshelp_node_t node, struct grub_fshelp_find_file_ctx *ctx)
62 {
63 if (node != ctx->rootnode)
64 grub_free (node);
65 }
66
67 static void
68 pop_element (struct grub_fshelp_find_file_ctx *ctx)
69 {
70 struct stack_element *el;
71 el = ctx->currnode;
72 ctx->currnode = el->parent;
73 free_node (el->node, ctx);
74 grub_free (el);
75 }
76
77 static void
78 free_stack (struct grub_fshelp_find_file_ctx *ctx)
79 {
80 while (ctx->currnode)
81 pop_element (ctx);
82 }
83
84 static void
85 go_up_a_level (struct grub_fshelp_find_file_ctx *ctx)
86 {
87 if (!ctx->currnode->parent)
88 return;
89 pop_element (ctx);
90 }
91
92 static grub_err_t
93 push_node (struct grub_fshelp_find_file_ctx *ctx, grub_fshelp_node_t node, enum grub_fshelp_filetype filetype)
94 {
95 struct stack_element *nst;
96 nst = grub_malloc (sizeof (*nst));
97 if (!nst)
98 return grub_errno;
99 nst->node = node;
100 nst->type = filetype & ~GRUB_FSHELP_CASE_INSENSITIVE;
101 nst->parent = ctx->currnode;
102 ctx->currnode = nst;
103 return GRUB_ERR_NONE;
104 }
105
106 static grub_err_t
107 go_to_root (struct grub_fshelp_find_file_ctx *ctx)
108 {
109 free_stack (ctx);
110 return push_node (ctx, ctx->rootnode, GRUB_FSHELP_DIR);
111 }
112
113 struct grub_fshelp_find_file_iter_ctx
114 {
115 const char *name;
116 grub_fshelp_node_t *foundnode;
117 enum grub_fshelp_filetype *foundtype;
118 };
119
120 int g_ventoy_case_insensitive = 0;
121
122 /* Helper for grub_fshelp_find_file. */
123 static int
124 find_file_iter (const char *filename, enum grub_fshelp_filetype filetype,
125 grub_fshelp_node_t node, void *data)
126 {
127 struct grub_fshelp_find_file_iter_ctx *ctx = data;
128
129 if (g_ventoy_case_insensitive)
130 {
131 filetype |= GRUB_FSHELP_CASE_INSENSITIVE;
132 }
133
134 if (filetype == GRUB_FSHELP_UNKNOWN ||
135 ((filetype & GRUB_FSHELP_CASE_INSENSITIVE)
136 ? grub_strcasecmp (ctx->name, filename)
137 : grub_strcmp (ctx->name, filename)))
138 {
139 grub_free (node);
140 return 0;
141 }
142
143 /* The node is found, stop iterating over the nodes. */
144 *ctx->foundnode = node;
145 *ctx->foundtype = filetype;
146 return 1;
147 }
148
149 static grub_err_t
150 directory_find_file (grub_fshelp_node_t node, const char *name, grub_fshelp_node_t *foundnode,
151 enum grub_fshelp_filetype *foundtype, iterate_dir_func iterate_dir)
152 {
153 int found;
154 struct grub_fshelp_find_file_iter_ctx ctx = {
155 .foundnode = foundnode,
156 .foundtype = foundtype,
157 .name = name
158 };
159 found = iterate_dir (node, find_file_iter, &ctx);
160 if (! found)
161 {
162 if (grub_errno)
163 return grub_errno;
164 }
165 return GRUB_ERR_NONE;
166 }
167
168 static grub_err_t
169 find_file (char *currpath,
170 iterate_dir_func iterate_dir, lookup_file_func lookup_file,
171 read_symlink_func read_symlink,
172 struct grub_fshelp_find_file_ctx *ctx)
173 {
174 char *name, *next;
175 grub_err_t err;
176 for (name = currpath; ; name = next)
177 {
178 char c;
179 grub_fshelp_node_t foundnode = NULL;
180 enum grub_fshelp_filetype foundtype = 0;
181
182 /* Remove all leading slashes. */
183 while (*name == '/')
184 name++;
185
186 /* Found the node! */
187 if (! *name)
188 return 0;
189
190 /* Extract the actual part from the pathname. */
191 for (next = name; *next && *next != '/'; next++);
192
193 /* At this point it is expected that the current node is a
194 directory, check if this is true. */
195 if (ctx->currnode->type != GRUB_FSHELP_DIR)
196 return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
197
198 /* Don't rely on fs providing actual . in the listing. */
199 if (next - name == 1 && name[0] == '.')
200 continue;
201
202 /* Don't rely on fs providing actual .. in the listing. */
203 if (next - name == 2 && name[0] == '.' && name[1] == '.')
204 {
205 go_up_a_level (ctx);
206 continue;
207 }
208
209 /* Iterate over the directory. */
210 c = *next;
211 *next = '\0';
212 if (lookup_file)
213 err = lookup_file (ctx->currnode->node, name, &foundnode, &foundtype);
214 else
215 err = directory_find_file (ctx->currnode->node, name, &foundnode, &foundtype, iterate_dir);
216 *next = c;
217
218 if (err)
219 return err;
220
221 if (!foundnode)
222 break;
223
224 push_node (ctx, foundnode, foundtype);
225
226 /* Read in the symlink and follow it. */
227 if (ctx->currnode->type == GRUB_FSHELP_SYMLINK)
228 {
229 char *symlink;
230
231 /* Test if the symlink does not loop. */
232 if (++ctx->symlinknest == 8)
233 return grub_error (GRUB_ERR_SYMLINK_LOOP,
234 N_("too deep nesting of symlinks"));
235
236 symlink = read_symlink (ctx->currnode->node);
237
238 if (!symlink)
239 return grub_errno;
240
241 /* The symlink is an absolute path, go back to the root inode. */
242 if (symlink[0] == '/')
243 {
244 err = go_to_root (ctx);
245 if (err)
246 return err;
247 }
248 else
249 {
250 /* Get from symlink to containing directory. */
251 go_up_a_level (ctx);
252 }
253
254
255 /* Lookup the node the symlink points to. */
256 find_file (symlink, iterate_dir, lookup_file, read_symlink, ctx);
257 grub_free (symlink);
258
259 if (grub_errno)
260 return grub_errno;
261 }
262 }
263
264 return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"),
265 ctx->path);
266 }
267
268 static grub_err_t
269 grub_fshelp_find_file_real (const char *path, grub_fshelp_node_t rootnode,
270 grub_fshelp_node_t *foundnode,
271 iterate_dir_func iterate_dir,
272 lookup_file_func lookup_file,
273 read_symlink_func read_symlink,
274 enum grub_fshelp_filetype expecttype)
275 {
276 struct grub_fshelp_find_file_ctx ctx = {
277 .path = path,
278 .rootnode = rootnode,
279 .symlinknest = 0,
280 .currnode = 0
281 };
282 grub_err_t err;
283 enum grub_fshelp_filetype foundtype;
284 char *duppath;
285
286 if (!path || path[0] != '/')
287 {
288 return grub_error (GRUB_ERR_BAD_FILENAME, N_("invalid file name `%s'"), path);
289 }
290
291 err = go_to_root (&ctx);
292 if (err)
293 return err;
294
295 duppath = grub_strdup (path);
296 if (!duppath)
297 return grub_errno;
298 err = find_file (duppath, iterate_dir, lookup_file, read_symlink, &ctx);
299 grub_free (duppath);
300 if (err)
301 {
302 free_stack (&ctx);
303 return err;
304 }
305
306 *foundnode = ctx.currnode->node;
307 foundtype = ctx.currnode->type;
308 /* Avoid the node being freed. */
309 ctx.currnode->node = 0;
310 free_stack (&ctx);
311
312 /* Check if the node that was found was of the expected type. */
313 if (expecttype == GRUB_FSHELP_REG && foundtype != expecttype)
314 return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a regular file"));
315 else if (expecttype == GRUB_FSHELP_DIR && foundtype != expecttype)
316 return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
317
318 return 0;
319 }
320
321 /* Lookup the node PATH. The node ROOTNODE describes the root of the
322 directory tree. The node found is returned in FOUNDNODE, which is
323 either a ROOTNODE or a new malloc'ed node. ITERATE_DIR is used to
324 iterate over all directory entries in the current node.
325 READ_SYMLINK is used to read the symlink if a node is a symlink.
326 EXPECTTYPE is the type node that is expected by the called, an
327 error is generated if the node is not of the expected type. */
328 grub_err_t
329 grub_fshelp_find_file (const char *path, grub_fshelp_node_t rootnode,
330 grub_fshelp_node_t *foundnode,
331 iterate_dir_func iterate_dir,
332 read_symlink_func read_symlink,
333 enum grub_fshelp_filetype expecttype)
334 {
335 return grub_fshelp_find_file_real (path, rootnode, foundnode,
336 iterate_dir, NULL,
337 read_symlink, expecttype);
338
339 }
340
341 grub_err_t
342 grub_fshelp_find_file_lookup (const char *path, grub_fshelp_node_t rootnode,
343 grub_fshelp_node_t *foundnode,
344 lookup_file_func lookup_file,
345 read_symlink_func read_symlink,
346 enum grub_fshelp_filetype expecttype)
347 {
348 return grub_fshelp_find_file_real (path, rootnode, foundnode,
349 NULL, lookup_file,
350 read_symlink, expecttype);
351
352 }
353
354 /* Read LEN bytes from the file NODE on disk DISK into the buffer BUF,
355 beginning with the block POS. READ_HOOK should be set before
356 reading a block from the file. READ_HOOK_DATA is passed through as
357 the DATA argument to READ_HOOK. GET_BLOCK is used to translate
358 file blocks to disk blocks. The file is FILESIZE bytes big and the
359 blocks have a size of LOG2BLOCKSIZE (in log2). */
360 grub_ssize_t
361 grub_fshelp_read_file (grub_disk_t disk, grub_fshelp_node_t node,
362 grub_disk_read_hook_t read_hook, void *read_hook_data,
363 grub_off_t pos, grub_size_t len, char *buf,
364 grub_disk_addr_t (*get_block) (grub_fshelp_node_t node,
365 grub_disk_addr_t block),
366 grub_off_t filesize, int log2blocksize,
367 grub_disk_addr_t blocks_start)
368 {
369 grub_disk_addr_t i, blockcnt;
370 int blocksize = 1 << (log2blocksize + GRUB_DISK_SECTOR_BITS);
371
372 if (pos > filesize)
373 {
374 grub_error (GRUB_ERR_OUT_OF_RANGE,
375 N_("attempt to read past the end of file"));
376 return -1;
377 }
378
379 /* Adjust LEN so it we can't read past the end of the file. */
380 if (pos + len > filesize)
381 len = filesize - pos;
382
383 blockcnt = ((len + pos) + blocksize - 1) >> (log2blocksize + GRUB_DISK_SECTOR_BITS);
384
385 for (i = pos >> (log2blocksize + GRUB_DISK_SECTOR_BITS); i < blockcnt; i++)
386 {
387 grub_disk_addr_t blknr;
388 int blockoff = pos & (blocksize - 1);
389 int blockend = blocksize;
390
391 int skipfirst = 0;
392
393 blknr = get_block (node, i);
394 if (grub_errno)
395 return -1;
396
397 blknr = blknr << log2blocksize;
398
399 /* Last block. */
400 if (i == blockcnt - 1)
401 {
402 blockend = (len + pos) & (blocksize - 1);
403
404 /* The last portion is exactly blocksize. */
405 if (! blockend)
406 blockend = blocksize;
407 }
408
409 /* First block. */
410 if (i == (pos >> (log2blocksize + GRUB_DISK_SECTOR_BITS)))
411 {
412 skipfirst = blockoff;
413 blockend -= skipfirst;
414 }
415
416 /* If the block number is 0 this block is not stored on disk but
417 is zero filled instead. */
418 if (blknr)
419 {
420 disk->read_hook = read_hook;
421 disk->read_hook_data = read_hook_data;
422
423 grub_disk_read (disk, blknr + blocks_start, skipfirst,
424 blockend, buf);
425 disk->read_hook = 0;
426 if (grub_errno)
427 return -1;
428 }
429 else if (read_hook != (void *)grub_disk_blocklist_read)
430 grub_memset (buf, 0, blockend);
431
432 buf += blocksize - skipfirst;
433 }
434
435 return len;
436 }