1 /* blocklist.c - print the block list of a file */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2006,2007 Free Software Foundation, Inc.
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.
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.
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/>.
21 #include <grub/misc.h>
22 #include <grub/file.h>
24 #include <grub/disk.h>
25 #include <grub/partition.h>
26 #include <grub/command.h>
27 #include <grub/i18n.h>
29 GRUB_MOD_LICENSE ("GPLv3+");
31 /* Context for grub_cmd_blocklist. */
34 unsigned long start_sector
;
37 grub_disk_addr_t part_start
;
40 /* Helper for grub_cmd_blocklist. */
42 print_blocklist (grub_disk_addr_t sector
, unsigned num
,
43 unsigned offset
, unsigned length
, struct blocklist_ctx
*ctx
)
45 if (ctx
->num_entries
++)
48 grub_printf ("%llu", (unsigned long long) (sector
- ctx
->part_start
));
50 grub_printf ("+%u", num
);
51 if (offset
!= 0 || length
!= 0)
52 grub_printf ("[%u-%u]", offset
, offset
+ length
);
55 /* Helper for grub_cmd_blocklist. */
57 read_blocklist (grub_disk_addr_t sector
, unsigned offset
, unsigned length
,
60 struct blocklist_ctx
*ctx
= data
;
62 if (ctx
->num_sectors
> 0)
64 if (ctx
->start_sector
+ ctx
->num_sectors
== sector
65 && offset
== 0 && length
>= GRUB_DISK_SECTOR_SIZE
)
67 ctx
->num_sectors
+= length
>> GRUB_DISK_SECTOR_BITS
;
68 sector
+= length
>> GRUB_DISK_SECTOR_BITS
;
69 length
&= (GRUB_DISK_SECTOR_SIZE
- 1);
74 print_blocklist (ctx
->start_sector
, ctx
->num_sectors
, 0, 0, ctx
);
80 unsigned l
= length
+ offset
;
81 l
&= (GRUB_DISK_SECTOR_SIZE
- 1);
83 print_blocklist (sector
, 0, offset
, l
, ctx
);
92 if (length
& (GRUB_DISK_SECTOR_SIZE
- 1))
94 if (length
>> GRUB_DISK_SECTOR_BITS
)
96 print_blocklist (sector
, length
>> GRUB_DISK_SECTOR_BITS
, 0, 0, ctx
);
97 sector
+= length
>> GRUB_DISK_SECTOR_BITS
;
99 print_blocklist (sector
, 0, 0, length
& (GRUB_DISK_SECTOR_SIZE
- 1), ctx
);
103 ctx
->start_sector
= sector
;
104 ctx
->num_sectors
= length
>> GRUB_DISK_SECTOR_BITS
;
109 grub_cmd_blocklist (grub_command_t cmd
__attribute__ ((unused
)),
110 int argc
, char **args
)
113 char buf
[GRUB_DISK_SECTOR_SIZE
];
114 struct blocklist_ctx ctx
= {
122 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
124 file
= grub_file_open (args
[0], GRUB_FILE_TYPE_PRINT_BLOCKLIST
125 | GRUB_FILE_TYPE_NO_DECOMPRESS
);
129 if (! file
->device
->disk
)
130 return grub_error (GRUB_ERR_BAD_DEVICE
,
131 "this command is available only for disk devices");
133 ctx
.part_start
= grub_partition_get_start (file
->device
->disk
->partition
);
135 file
->read_hook
= read_blocklist
;
136 file
->read_hook_data
= &ctx
;
138 while (grub_file_read (file
, buf
, sizeof (buf
)) > 0)
141 if (ctx
.num_sectors
> 0)
142 print_blocklist (ctx
.start_sector
, ctx
.num_sectors
, 0, 0, &ctx
);
144 grub_printf("\nentry number:%d \n", ctx
.num_entries
);
146 grub_file_close (file
);
151 static grub_command_t cmd
;
153 GRUB_MOD_INIT(blocklist
)
155 cmd
= grub_register_command ("blocklist", grub_cmd_blocklist
,
156 N_("FILE"), N_("Print a block list."));
159 GRUB_MOD_FINI(blocklist
)
161 grub_unregister_command (cmd
);