]> git.proxmox.com Git - grub2.git/blame - grub-core/commands/cmp.c
verifiers: File type for fine-grained signature-verification controlling
[grub2.git] / grub-core / commands / cmp.c
CommitLineData
db1771cf 1/* cmd.c - command to cmp an operating system */
2/*
4b13b216 3 * GRUB -- GRand Unified Bootloader
58bc8bd5 4 * Copyright (C) 2003,2005,2006,2007,2009 Free Software Foundation, Inc.
db1771cf 5 *
5a79f472 6 * GRUB is free software: you can redistribute it and/or modify
db1771cf 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
db1771cf 9 * (at your option) any later version.
10 *
5a79f472 11 * GRUB is distributed in the hope that it will be useful,
db1771cf 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/>.
db1771cf 18 */
19
4b13b216 20#include <grub/dl.h>
4b13b216 21#include <grub/misc.h>
22#include <grub/file.h>
68c864eb 23#include <grub/mm.h>
b1b797cb 24#include <grub/command.h>
77a79592 25#include <grub/i18n.h>
68c864eb 26
e745cf0c
VS
27GRUB_MOD_LICENSE ("GPLv3+");
28
68c864eb 29#define BUFFER_SIZE 512
db1771cf 30
4b13b216 31static grub_err_t
b1b797cb 32grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)),
db1771cf 33 int argc, char **args)
34{
68c864eb 35 grub_ssize_t rd1, rd2;
524a1e6a 36 grub_off_t pos;
68c864eb 37 grub_file_t file1 = 0;
38 grub_file_t file2 = 0;
39 char *buf1 = 0;
40 char *buf2 = 0;
db1771cf 41
42 if (argc != 2)
9c4b5c13 43 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
db1771cf 44
6e0632e2
VS
45 grub_printf_ (N_("Compare file `%s' with `%s':\n"), args[0],
46 args[1]);
db1771cf 47
ca0a4f68
VS
48 file1 = grub_file_open (args[0], GRUB_FILE_TYPE_CMP);
49 file2 = grub_file_open (args[1], GRUB_FILE_TYPE_CMP);
524a1e6a 50 if (! file1 || ! file2)
68c864eb 51 goto cleanup;
db1771cf 52
4b13b216 53 if (grub_file_size (file1) != grub_file_size (file2))
6e0632e2
VS
54 grub_printf_ (N_("Files differ in size: %llu [%s], %llu [%s]\n"),
55 (unsigned long long) grub_file_size (file1), args[0],
56 (unsigned long long) grub_file_size (file2), args[1]);
db1771cf 57 else
58 {
68c864eb 59 pos = 0;
60
524a1e6a 61 buf1 = grub_malloc (BUFFER_SIZE);
62 buf2 = grub_malloc (BUFFER_SIZE);
b39f9d20 63
524a1e6a 64 if (! buf1 || ! buf2)
68c864eb 65 goto cleanup;
b39f9d20 66
db1771cf 67 do
68 {
69 int i;
b39f9d20 70
68c864eb 71 rd1 = grub_file_read (file1, buf1, BUFFER_SIZE);
72 rd2 = grub_file_read (file2, buf2, BUFFER_SIZE);
db1771cf 73
74 if (rd1 != rd2)
68c864eb 75 goto cleanup;
db1771cf 76
68c864eb 77 for (i = 0; i < rd2; i++)
db1771cf 78 {
79 if (buf1[i] != buf2[i])
80 {
6e0632e2
VS
81 grub_printf_ (N_("Files differ at the offset %llu: 0x%x [%s], 0x%x [%s]\n"),
82 (unsigned long long) (i + pos), buf1[i],
83 args[0], buf2[i], args[1]);
68c864eb 84 goto cleanup;
db1771cf 85 }
86 }
68c864eb 87 pos += BUFFER_SIZE;
b39f9d20 88
524a1e6a 89 }
90 while (rd2);
b39f9d20 91
6e0632e2
VS
92 /* TRANSLATORS: it's always exactly 2 files. */
93 grub_printf_ (N_("The files are identical.\n"));
db1771cf 94 }
95
68c864eb 96cleanup:
b39f9d20 97
cbf597af
SJ
98 grub_free (buf1);
99 grub_free (buf2);
68c864eb 100 if (file1)
101 grub_file_close (file1);
102 if (file2)
103 grub_file_close (file2);
104
524a1e6a 105 return grub_errno;
db1771cf 106}
107
b1b797cb 108static grub_command_t cmd;
db1771cf 109\f
6d099807 110GRUB_MOD_INIT(cmp)
db1771cf 111{
b1b797cb 112 cmd = grub_register_command ("cmp", grub_cmd_cmp,
77a79592 113 N_("FILE1 FILE2"), N_("Compare two files."));
db1771cf 114}
115
6d099807 116GRUB_MOD_FINI(cmp)
db1771cf 117{
b1b797cb 118 grub_unregister_command (cmd);
db1771cf 119}