]> git.proxmox.com Git - grub2.git/blame - grub-core/commands/testload.c
* grub-core/commands/ls.c (grub_ls_list_devices): Disable
[grub2.git] / grub-core / commands / testload.c
CommitLineData
6d3d698d 1/* testload.c - load the same file in multiple ways */
06f70117
VS
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
e745cf0c
VS
32GRUB_MOD_LICENSE ("GPLv3+");
33
06f70117
VS
34static grub_err_t
35grub_cmd_testload (struct grub_command *cmd __attribute__ ((unused)),
36 int argc, char *argv[])
37{
38 grub_file_t file;
39 char *buf;
40 grub_size_t size;
41 grub_off_t pos;
42 auto void NESTED_FUNC_ATTR read_func (grub_disk_addr_t sector, unsigned offset, unsigned len);
43
44 void NESTED_FUNC_ATTR read_func (grub_disk_addr_t sector __attribute__ ((unused)),
45 unsigned offset __attribute__ ((unused)),
46 unsigned len __attribute__ ((unused)))
47 {
48 grub_xputs (".");
49 grub_refresh ();
50 }
51
52 if (argc < 1)
9c4b5c13 53 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
06f70117
VS
54
55 file = grub_file_open (argv[0]);
56 if (! file)
57 return grub_errno;
58
59 size = grub_file_size (file) & ~(GRUB_DISK_SECTOR_SIZE - 1);
60 if (size == 0)
61 {
62 grub_file_close (file);
63 return GRUB_ERR_NONE;
64 }
65
66 buf = grub_malloc (size);
67 if (! buf)
68 goto fail;
69
70 grub_printf ("Reading %s sequentially", argv[0]);
71 file->read_hook = read_func;
72 if (grub_file_read (file, buf, size) != (grub_ssize_t) size)
73 goto fail;
74 grub_printf (" Done.\n");
75
76 /* Read sequentially again. */
77 grub_printf ("Reading %s sequentially again", argv[0]);
78 grub_file_seek (file, 0);
79
4e27343f 80 for (pos = 0; pos < size;)
06f70117
VS
81 {
82 char sector[GRUB_DISK_SECTOR_SIZE];
4e27343f 83 grub_size_t curlen = GRUB_DISK_SECTOR_SIZE;
06f70117 84
4e27343f
VS
85 if (curlen > size - pos)
86 curlen = size - pos;
87
88 if (grub_file_read (file, sector, curlen)
89 != (grub_ssize_t) curlen)
06f70117
VS
90 goto fail;
91
4e27343f 92 if (grub_memcmp (sector, buf + pos, curlen) != 0)
06f70117
VS
93 {
94 grub_printf ("\nDiffers in %lld\n", (unsigned long long) pos);
95 goto fail;
96 }
4e27343f 97 pos += curlen;
06f70117
VS
98 }
99 grub_printf (" Done.\n");
100
101 /* Read backwards and compare. */
102 grub_printf ("Reading %s backwards", argv[0]);
103 pos = size;
104 while (pos > 0)
105 {
106 char sector[GRUB_DISK_SECTOR_SIZE];
107
0ccb6b3c
VS
108 if (pos >= GRUB_DISK_SECTOR_SIZE)
109 pos -= GRUB_DISK_SECTOR_SIZE;
110 else
111 pos = 0;
06f70117
VS
112
113 grub_file_seek (file, pos);
114
115 if (grub_file_read (file, sector, GRUB_DISK_SECTOR_SIZE)
116 != GRUB_DISK_SECTOR_SIZE)
117 goto fail;
118
119 if (grub_memcmp (sector, buf + pos, GRUB_DISK_SECTOR_SIZE) != 0)
120 {
121 int i;
122
123 grub_printf ("\nDiffers in %lld\n", (unsigned long long) pos);
124
125 for (i = 0; i < GRUB_DISK_SECTOR_SIZE; i++)
126 {
127 grub_printf ("%02x ", buf[pos + i]);
128 if ((i & 15) == 15)
129 grub_printf ("\n");
130 }
131
132 if (i)
133 grub_refresh ();
134
135 goto fail;
136 }
137 }
138 grub_printf (" Done.\n");
139
140 return GRUB_ERR_NONE;
141
142 fail:
143
144 grub_file_close (file);
145 grub_free (buf);
146
147 if (!grub_errno)
148 grub_error (GRUB_ERR_IO, "bad read");
149 return grub_errno;
150}
151
152static grub_command_t cmd;
153
154GRUB_MOD_INIT(testload)
155{
156 cmd =
157 grub_register_command ("testload", grub_cmd_testload,
158 N_("FILE"),
159 N_("Load the same file in multiple ways."));
160}
161
162GRUB_MOD_FINI(testload)
163{
164 grub_unregister_command (cmd);
165}