]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/grub-2.04/grub-core/commands/blocklist.c
Update README.md
[Ventoy.git] / GRUB2 / grub-2.04 / grub-core / commands / blocklist.c
1 /* blocklist.c - print the block list of a file */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2006,2007 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/dl.h>
21 #include <grub/misc.h>
22 #include <grub/file.h>
23 #include <grub/mm.h>
24 #include <grub/disk.h>
25 #include <grub/partition.h>
26 #include <grub/command.h>
27 #include <grub/i18n.h>
28
29 GRUB_MOD_LICENSE ("GPLv3+");
30
31 /* Context for grub_cmd_blocklist. */
32 struct blocklist_ctx
33 {
34 unsigned long start_sector;
35 unsigned num_sectors;
36 int num_entries;
37 grub_disk_addr_t part_start;
38 };
39
40 /* Helper for grub_cmd_blocklist. */
41 static void
42 print_blocklist (grub_disk_addr_t sector, unsigned num,
43 unsigned offset, unsigned length, struct blocklist_ctx *ctx)
44 {
45 if (ctx->num_entries++)
46 grub_printf (",");
47
48 grub_printf ("%llu", (unsigned long long) (sector - ctx->part_start));
49 if (num > 0)
50 grub_printf ("+%u", num);
51 if (offset != 0 || length != 0)
52 grub_printf ("[%u-%u]", offset, offset + length);
53 }
54
55 /* Helper for grub_cmd_blocklist. */
56 static void
57 read_blocklist (grub_disk_addr_t sector, unsigned offset, unsigned length,
58 void *data)
59 {
60 struct blocklist_ctx *ctx = data;
61
62 if (ctx->num_sectors > 0)
63 {
64 if (ctx->start_sector + ctx->num_sectors == sector
65 && offset == 0 && length >= GRUB_DISK_SECTOR_SIZE)
66 {
67 ctx->num_sectors += length >> GRUB_DISK_SECTOR_BITS;
68 sector += length >> GRUB_DISK_SECTOR_BITS;
69 length &= (GRUB_DISK_SECTOR_SIZE - 1);
70 }
71
72 if (!length)
73 return;
74 print_blocklist (ctx->start_sector, ctx->num_sectors, 0, 0, ctx);
75 ctx->num_sectors = 0;
76 }
77
78 if (offset)
79 {
80 unsigned l = length + offset;
81 l &= (GRUB_DISK_SECTOR_SIZE - 1);
82 l -= offset;
83 print_blocklist (sector, 0, offset, l, ctx);
84 length -= l;
85 sector++;
86 offset = 0;
87 }
88
89 if (!length)
90 return;
91
92 if (length & (GRUB_DISK_SECTOR_SIZE - 1))
93 {
94 if (length >> GRUB_DISK_SECTOR_BITS)
95 {
96 print_blocklist (sector, length >> GRUB_DISK_SECTOR_BITS, 0, 0, ctx);
97 sector += length >> GRUB_DISK_SECTOR_BITS;
98 }
99 print_blocklist (sector, 0, 0, length & (GRUB_DISK_SECTOR_SIZE - 1), ctx);
100 }
101 else
102 {
103 ctx->start_sector = sector;
104 ctx->num_sectors = length >> GRUB_DISK_SECTOR_BITS;
105 }
106 }
107
108 static grub_err_t
109 grub_cmd_blocklist (grub_command_t cmd __attribute__ ((unused)),
110 int argc, char **args)
111 {
112 grub_file_t file;
113 char buf[GRUB_DISK_SECTOR_SIZE];
114 struct blocklist_ctx ctx = {
115 .start_sector = 0,
116 .num_sectors = 0,
117 .num_entries = 0,
118 .part_start = 0
119 };
120
121 if (argc < 1)
122 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
123
124 file = grub_file_open (args[0], GRUB_FILE_TYPE_PRINT_BLOCKLIST
125 | GRUB_FILE_TYPE_NO_DECOMPRESS);
126 if (! file)
127 return grub_errno;
128
129 if (! file->device->disk)
130 return grub_error (GRUB_ERR_BAD_DEVICE,
131 "this command is available only for disk devices");
132
133 ctx.part_start = grub_partition_get_start (file->device->disk->partition);
134
135 file->read_hook = read_blocklist;
136 file->read_hook_data = &ctx;
137
138 while (grub_file_read (file, buf, sizeof (buf)) > 0)
139 ;
140
141 if (ctx.num_sectors > 0)
142 print_blocklist (ctx.start_sector, ctx.num_sectors, 0, 0, &ctx);
143
144 grub_printf("\nentry number:%d \n", ctx.num_entries);
145
146 grub_file_close (file);
147
148 return grub_errno;
149 }
150
151 static grub_command_t cmd;
152 \f
153 GRUB_MOD_INIT(blocklist)
154 {
155 cmd = grub_register_command ("blocklist", grub_cmd_blocklist,
156 N_("FILE"), N_("Print a block list."));
157 }
158
159 GRUB_MOD_FINI(blocklist)
160 {
161 grub_unregister_command (cmd);
162 }