4 * Copyright (C) 2012 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
18 #include <sys/types.h>
28 void do_zap_block(int argc, char *argv[])
30 unsigned long pattern = 0;
43 if (check_fs_open(argv[0]))
45 if (check_fs_read_write(argv[0]))
49 while ((c = getopt (argc, argv, "b:f:l:o:p:")) != EOF) {
55 bit = parse_ulong(optarg, argv[0],
59 if (bit >= current_fs->blocksize * 8) {
60 com_err(argv[0], 0, "The bit to flip "
61 "must be within a %d block\n",
62 current_fs->blocksize);
67 pattern = parse_ulong(optarg, argv[0],
72 com_err(argv[0], 0, "The fill pattern must "
73 "be an 8-bit value\n");
78 offset = parse_ulong(optarg, argv[0],
82 if (offset >= current_fs->blocksize) {
83 com_err(argv[0], 0, "The offset must be "
84 "within a %d block\n",
85 current_fs->blocksize);
92 length = parse_ulong(optarg, argv[0],
102 if (bit > 0 && offset > 0) {
103 com_err(argv[0], 0, "The -o and -b options can not be mixed.");
110 length = current_fs->blocksize - offset;
111 if ((offset + length) > current_fs->blocksize) {
112 com_err(argv[0], 0, "The specified length is too bug\n");
116 if (argc != optind+1) {
118 com_err(0, 0, "Usage:\tzap_block [-f file] [-o offset] "
119 "[-l length] [-p pattern] block_num");
120 com_err(0, 0, "\tzap_block [-f file] [-b bit] "
125 block = parse_ulonglong(argv[optind], argv[0], "block", &err);
130 inode = string_to_inode(file);
133 errcode = ext2fs_bmap2(current_fs, inode, 0, 0, 0,
136 com_err(argv[0], errcode,
137 "while mapping logical block %llu\n", block);
142 buf = malloc(current_fs->blocksize);
144 com_err(argv[0], 0, "Couldn't allocate block buffer");
148 errcode = io_channel_read_blk64(current_fs->io, block, 1, buf);
150 com_err(argv[0], errcode,
151 "while reading block %llu\n", block);
156 buf[bit >> 3] ^= 1 << (bit & 7);
158 memset(buf+offset, pattern, length);
160 errcode = io_channel_write_blk64(current_fs->io, block, 1, buf);
162 com_err(argv[0], errcode,
163 "while write block %llu\n", block);
172 void do_dump_block(int argc, char *argv[])
174 unsigned long pattern = 0;
181 char *in_file = NULL;
185 if (check_fs_open(argv[0]))
189 while ((c = getopt (argc, argv, "f:")) != EOF) {
200 if (argc != optind+1) {
202 com_err(0, 0, "Usage: dump_block [-f file] "
207 block = parse_ulonglong(argv[optind], argv[0], "block", &err);
212 inode = string_to_inode(file);
215 errcode = ext2fs_bmap2(current_fs, inode, 0, 0, 0,
218 com_err(argv[0], errcode,
219 "while mapping logical block %llu\n", block);
224 buf = malloc(current_fs->blocksize);
226 com_err(argv[0], 0, "Couldn't allocate block buffer");
230 errcode = io_channel_read_blk64(current_fs->io, block, 1, buf);
232 com_err(argv[0], errcode,
233 "while reading block %llu\n", block);
237 for (i=0; i < current_fs->blocksize; i += 16) {
239 if (i && memcmp(buf + i, buf + i - 16, 16) == 0) {
245 if (memcmp(buf + i, buf + suppress, 16) == 0)
250 for (j = 0; j < 16; j++) {
251 printf("%02x", buf[i+j]);
256 for (j = 0; j < 16; j++)
257 printf("%c", isprint(buf[i+j]) ? buf[i+j] : '.');