]> git.proxmox.com Git - grub2.git/blob - grub-core/commands/testload.c
Import grub2_2.02+dfsg1.orig.tar.xz
[grub2.git] / grub-core / commands / testload.c
1 /* testload.c - load the same file in multiple ways */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005,2006,2007,2009,2010 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/mm.h>
22 #include <grub/err.h>
23 #include <grub/env.h>
24 #include <grub/misc.h>
25 #include <grub/file.h>
26 #include <grub/disk.h>
27 #include <grub/term.h>
28 #include <grub/loader.h>
29 #include <grub/command.h>
30 #include <grub/i18n.h>
31
32 GRUB_MOD_LICENSE ("GPLv3+");
33
34 /* Helper for grub_cmd_testload. */
35 static void
36 read_progress (grub_disk_addr_t sector __attribute__ ((unused)),
37 unsigned offset __attribute__ ((unused)),
38 unsigned len,
39 void *data __attribute__ ((unused)))
40 {
41 for (; len >= GRUB_DISK_SECTOR_SIZE; len -= GRUB_DISK_SECTOR_SIZE)
42 grub_xputs (".");
43 if (len)
44 grub_xputs (".");
45 grub_refresh ();
46 }
47
48 static grub_err_t
49 grub_cmd_testload (struct grub_command *cmd __attribute__ ((unused)),
50 int argc, char *argv[])
51 {
52 grub_file_t file;
53 char *buf;
54 grub_size_t size;
55 grub_off_t pos;
56
57 if (argc < 1)
58 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
59
60 file = grub_file_open (argv[0]);
61 if (! file)
62 return grub_errno;
63
64 size = grub_file_size (file) & ~(GRUB_DISK_SECTOR_SIZE - 1);
65 if (size == 0)
66 {
67 grub_file_close (file);
68 return GRUB_ERR_NONE;
69 }
70
71 buf = grub_malloc (size);
72 if (! buf)
73 goto fail;
74
75 grub_printf ("Reading %s sequentially", argv[0]);
76 file->read_hook = read_progress;
77 if (grub_file_read (file, buf, size) != (grub_ssize_t) size)
78 goto fail;
79 grub_printf (" Done.\n");
80
81 /* Read sequentially again. */
82 grub_printf ("Reading %s sequentially again", argv[0]);
83 grub_file_seek (file, 0);
84
85 for (pos = 0; pos < size;)
86 {
87 char sector[GRUB_DISK_SECTOR_SIZE];
88 grub_size_t curlen = GRUB_DISK_SECTOR_SIZE;
89
90 if (curlen > size - pos)
91 curlen = size - pos;
92
93 if (grub_file_read (file, sector, curlen)
94 != (grub_ssize_t) curlen)
95 goto fail;
96
97 if (grub_memcmp (sector, buf + pos, curlen) != 0)
98 {
99 grub_printf ("\nDiffers in %lld\n", (unsigned long long) pos);
100 goto fail;
101 }
102 pos += curlen;
103 }
104 grub_printf (" Done.\n");
105
106 /* Read backwards and compare. */
107 grub_printf ("Reading %s backwards", argv[0]);
108 pos = size;
109 while (pos > 0)
110 {
111 char sector[GRUB_DISK_SECTOR_SIZE];
112
113 if (pos >= GRUB_DISK_SECTOR_SIZE)
114 pos -= GRUB_DISK_SECTOR_SIZE;
115 else
116 pos = 0;
117
118 grub_file_seek (file, pos);
119
120 if (grub_file_read (file, sector, GRUB_DISK_SECTOR_SIZE)
121 != GRUB_DISK_SECTOR_SIZE)
122 goto fail;
123
124 if (grub_memcmp (sector, buf + pos, GRUB_DISK_SECTOR_SIZE) != 0)
125 {
126 int i;
127
128 grub_printf ("\nDiffers in %lld\n", (unsigned long long) pos);
129
130 for (i = 0; i < GRUB_DISK_SECTOR_SIZE; i++)
131 {
132 grub_printf ("%02x ", buf[pos + i]);
133 if ((i & 15) == 15)
134 grub_printf ("\n");
135 }
136
137 if (i)
138 grub_refresh ();
139
140 goto fail;
141 }
142 }
143 grub_printf (" Done.\n");
144
145 return GRUB_ERR_NONE;
146
147 fail:
148
149 grub_file_close (file);
150 grub_free (buf);
151
152 if (!grub_errno)
153 grub_error (GRUB_ERR_IO, "bad read");
154 return grub_errno;
155 }
156
157 static grub_command_t cmd;
158
159 GRUB_MOD_INIT(testload)
160 {
161 cmd =
162 grub_register_command ("testload", grub_cmd_testload,
163 N_("FILE"),
164 N_("Load the same file in multiple ways."));
165 }
166
167 GRUB_MOD_FINI(testload)
168 {
169 grub_unregister_command (cmd);
170 }