]> git.proxmox.com Git - grub2.git/blame - commands/blocklist.c
2006-05-27 Yoshinori K. Okuji <okuji@enbug.org>
[grub2.git] / commands / blocklist.c
CommitLineData
89a7d726 1/* blocklist.c - print the block list of a file */
2/*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2006 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 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <grub/normal.h>
22#include <grub/dl.h>
23#include <grub/arg.h>
24#include <grub/misc.h>
25#include <grub/file.h>
26#include <grub/mm.h>
27#include <grub/disk.h>
28
29static grub_err_t
30grub_cmd_blocklist (struct grub_arg_list *state __attribute__ ((unused)),
31 int argc, char **args)
32{
33 grub_file_t file;
34 char buf[GRUB_DISK_SECTOR_SIZE];
35 unsigned long start_sector = 0;
36 unsigned num_sectors = 0;
37 int num_entries = 0;
38 auto void read_blocklist (unsigned long sector, unsigned offset,
39 unsigned length);
40 auto void print_blocklist (unsigned long sector, unsigned num,
41 unsigned offset, unsigned length);
42
43 void read_blocklist (unsigned long sector, unsigned offset,
44 unsigned length)
45 {
46 if (num_sectors > 0)
47 {
48 if (start_sector + num_sectors == sector
49 && offset == 0 && length == GRUB_DISK_SECTOR_SIZE)
50 {
51 num_sectors++;
52 return;
53 }
54
55 print_blocklist (start_sector, num_sectors, 0, 0);
56 num_sectors = 0;
57 }
58
59 if (offset == 0 && length == GRUB_DISK_SECTOR_SIZE)
60 {
61 start_sector = sector;
62 num_sectors++;
63 }
64 else
65 print_blocklist (sector, 0, offset, length);
66 }
67
68 void print_blocklist (unsigned long sector, unsigned num,
69 unsigned offset, unsigned length)
70 {
71 if (num_entries++)
72 grub_printf (",");
73
74 grub_printf ("%lu", sector);
75 if (num > 0)
76 grub_printf ("+%u", num);
77 if (offset != 0 || length != 0)
78 grub_printf ("[%u-%u]", offset, offset + length);
79 }
80
81 if (argc < 1)
82 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
83
84 file = grub_file_open (args[0]);
85 if (! file)
86 return grub_errno;
87
88 file->read_hook = read_blocklist;
89
90 while (grub_file_read (file, buf, sizeof (buf)) > 0)
91 ;
92
93 if (num_sectors > 0)
94 print_blocklist (start_sector, num_sectors, 0, 0);
95
96 grub_file_close (file);
97
98 return grub_errno;
99}
100
101\f
102GRUB_MOD_INIT(blocklist)
103{
104 (void) mod; /* To stop warning. */
105 grub_register_command ("blocklist", grub_cmd_blocklist,
106 GRUB_COMMAND_FLAG_BOTH,
107 "blocklist FILE",
108 "Print a block list.", 0);
109}
110
111GRUB_MOD_FINI(blocklist)
112{
113 grub_unregister_command ("blocklist");
114}