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[], int sci_idx EXT2FS_ATTR((unused)),
29 void *infop EXT2FS_ATTR((unused)))
31 unsigned long pattern = 0;
42 if (check_fs_open(argv[0]))
44 if (check_fs_read_write(argv[0]))
48 while ((c = getopt (argc, argv, "b:f:l:o:p:")) != EOF) {
54 bit = parse_ulong(optarg, argv[0],
58 if (bit >= (int) current_fs->blocksize * 8) {
59 com_err(argv[0], 0, "The bit to flip "
60 "must be within a %d block\n",
61 current_fs->blocksize);
66 pattern = parse_ulong(optarg, argv[0],
71 com_err(argv[0], 0, "The fill pattern must "
72 "be an 8-bit value\n");
77 offset = parse_ulong(optarg, argv[0],
81 if (offset >= (int) current_fs->blocksize) {
82 com_err(argv[0], 0, "The offset must be "
83 "within a %d block\n",
84 current_fs->blocksize);
91 length = parse_ulong(optarg, argv[0],
101 if (bit > 0 && offset > 0) {
102 com_err(argv[0], 0, "The -o and -b options can not be mixed.");
109 length = current_fs->blocksize - offset;
110 if ((offset + length) > (int) current_fs->blocksize) {
111 com_err(argv[0], 0, "The specified length is too bug\n");
115 if (argc != optind+1) {
117 com_err(0, 0, "Usage:\tzap_block [-f file] [-o offset] "
118 "[-l length] [-p pattern] block_num");
119 com_err(0, 0, "\tzap_block [-f file] [-b bit] "
124 block = parse_ulonglong(argv[optind], argv[0], "block", &err);
129 inode = string_to_inode(file);
132 errcode = ext2fs_bmap2(current_fs, inode, 0, 0, 0,
135 com_err(argv[0], errcode,
136 "while mapping logical block %llu\n", block);
141 buf = malloc(current_fs->blocksize);
143 com_err(argv[0], 0, "Couldn't allocate block buffer");
147 errcode = io_channel_read_blk64(current_fs->io, block, 1, buf);
149 com_err(argv[0], errcode,
150 "while reading block %llu\n", block);
155 buf[bit >> 3] ^= 1 << (bit & 7);
157 memset(buf+offset, pattern, length);
159 errcode = io_channel_write_blk64(current_fs->io, block, 1, buf);
161 com_err(argv[0], errcode,
162 "while write block %llu\n", block);
171 void do_block_dump(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
172 void *infop EXT2FS_ATTR((unused)))
182 if (check_fs_open(argv[0]))
186 while ((c = getopt (argc, argv, "f:x")) != EOF) {
199 if (argc != optind + 1) {
201 com_err(0, 0, "Usage: block_dump [-x] [-f inode] block_num");
205 block = parse_ulonglong(argv[optind], argv[0], "block", &err);
210 inode = string_to_inode(file);
213 errcode = ext2fs_bmap2(current_fs, inode, 0, 0, 0,
216 com_err(argv[0], errcode,
217 "while mapping logical block %llu\n", block);
222 buf = malloc(current_fs->blocksize);
224 com_err(argv[0], 0, "Couldn't allocate block buffer");
228 errcode = io_channel_read_blk64(current_fs->io, block, 1, buf);
230 com_err(argv[0], errcode,
231 "while reading block %llu\n", block);
236 block_xattr_dump(stdout, buf, current_fs->blocksize);
238 do_byte_hexdump(stdout, buf, current_fs->blocksize);