]> git.proxmox.com Git - grub2.git/blame - grub-core/commands/blocklist.c
malloc: Use overflow checking primitives where we do complex allocations
[grub2.git] / grub-core / commands / blocklist.c
CommitLineData
89a7d726 1/* blocklist.c - print the block list of a file */
2/*
3 * GRUB -- GRand Unified Bootloader
5a79f472 4 * Copyright (C) 2006,2007 Free Software Foundation, Inc.
89a7d726 5 *
5a79f472 6 * GRUB is free software: you can redistribute it and/or modify
89a7d726 7 * it under the terms of the GNU General Public License as published by
5a79f472 8 * the Free Software Foundation, either version 3 of the License, or
89a7d726 9 * (at your option) any later version.
10 *
5a79f472 11 * GRUB is distributed in the hope that it will be useful,
89a7d726 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
5a79f472 17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
89a7d726 18 */
19
89a7d726 20#include <grub/dl.h>
89a7d726 21#include <grub/misc.h>
22#include <grub/file.h>
23#include <grub/mm.h>
24#include <grub/disk.h>
524a1e6a 25#include <grub/partition.h>
b1b797cb 26#include <grub/command.h>
77a79592 27#include <grub/i18n.h>
89a7d726 28
e745cf0c
VS
29GRUB_MOD_LICENSE ("GPLv3+");
30
4eb8b756
CW
31/* Context for grub_cmd_blocklist. */
32struct blocklist_ctx
89a7d726 33{
4eb8b756
CW
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. */
41static void
42print_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. */
56static void
57read_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)
89a7d726 63 {
4eb8b756 64 if (ctx->start_sector + ctx->num_sectors == sector
cb72aa18 65 && offset == 0 && length >= GRUB_DISK_SECTOR_SIZE)
89a7d726 66 {
cb72aa18
VS
67 ctx->num_sectors += length >> GRUB_DISK_SECTOR_BITS;
68 sector += length >> GRUB_DISK_SECTOR_BITS;
69 length &= (GRUB_DISK_SECTOR_SIZE - 1);
89a7d726 70 }
71
cb72aa18
VS
72 if (!length)
73 return;
4eb8b756
CW
74 print_blocklist (ctx->start_sector, ctx->num_sectors, 0, 0, ctx);
75 ctx->num_sectors = 0;
89a7d726 76 }
b39f9d20 77
cb72aa18 78 if (offset)
89a7d726 79 {
cb72aa18
VS
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);
89a7d726 100 }
4eb8b756 101 else
cb72aa18
VS
102 {
103 ctx->start_sector = sector;
104 ctx->num_sectors = length >> GRUB_DISK_SECTOR_BITS;
105 }
4eb8b756
CW
106}
107
108static grub_err_t
109grub_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 };
b39f9d20 120
89a7d726 121 if (argc < 1)
9c4b5c13 122 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
89a7d726 123
ca0a4f68
VS
124 file = grub_file_open (args[0], GRUB_FILE_TYPE_PRINT_BLOCKLIST
125 | GRUB_FILE_TYPE_NO_DECOMPRESS);
89a7d726 126 if (! file)
127 return grub_errno;
128
524a1e6a 129 if (! file->device->disk)
130 return grub_error (GRUB_ERR_BAD_DEVICE,
7fd0baee 131 "this command is available only for disk devices");
524a1e6a 132
4eb8b756 133 ctx.part_start = grub_partition_get_start (file->device->disk->partition);
b39f9d20 134
89a7d726 135 file->read_hook = read_blocklist;
4eb8b756 136 file->read_hook_data = &ctx;
89a7d726 137
138 while (grub_file_read (file, buf, sizeof (buf)) > 0)
139 ;
140
4eb8b756
CW
141 if (ctx.num_sectors > 0)
142 print_blocklist (ctx.start_sector, ctx.num_sectors, 0, 0, &ctx);
b39f9d20 143
89a7d726 144 grub_file_close (file);
145
146 return grub_errno;
147}
148
b1b797cb 149static grub_command_t cmd;
89a7d726 150\f
151GRUB_MOD_INIT(blocklist)
152{
b1b797cb 153 cmd = grub_register_command ("blocklist", grub_cmd_blocklist,
77a79592 154 N_("FILE"), N_("Print a block list."));
89a7d726 155}
156
157GRUB_MOD_FINI(blocklist)
158{
b1b797cb 159 grub_unregister_command (cmd);
89a7d726 160}