2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
17 #include "kerncompat.h"
21 #include <sys/fcntl.h>
27 #include <kernel-lib/crc32c.h>
30 #define BLOCKSIZE (4096)
31 static char buf[BLOCKSIZE];
34 static int check_csum_superblock(void *sb)
39 crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE,
40 crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
41 btrfs_csum_final(crc, result);
43 return !memcmp(sb, &result, csum_size);
46 static void update_block_csum(void *block, int is_sb)
49 struct btrfs_header *hdr;
53 crc = btrfs_csum_data((char *)block + BTRFS_CSUM_SIZE, crc,
54 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
56 crc = btrfs_csum_data((char *)block + BTRFS_CSUM_SIZE, crc,
57 BLOCKSIZE - BTRFS_CSUM_SIZE);
59 btrfs_csum_final(crc, result);
60 memset(block, 0, BTRFS_CSUM_SIZE);
61 hdr = (struct btrfs_header *)block;
62 memcpy(&hdr->csum, result, csum_size);
65 static u64 arg_strtou64(const char *str)
68 char *ptr_parse_end = NULL;
70 value = strtoull(str, &ptr_parse_end, 0);
71 if (ptr_parse_end && *ptr_parse_end != '\0') {
72 fprintf(stderr, "ERROR: %s is not a valid numeric value.\n",
78 * if we pass a negative number to strtoull, it will return an
79 * unexpected number to us, so let's do the check ourselves.
82 fprintf(stderr, "ERROR: %s: negative value is invalid.\n",
86 if (value == ULLONG_MAX) {
87 fprintf(stderr, "ERROR: %s is too large.\n", str);
100 OP_NAND, /* broken */
117 enum field_type type;
119 { .name = "total_bytes", .type = TYPE_U64 },
120 { .name = "root", .type = TYPE_U64 },
121 { .name = "generation", .type = TYPE_U64 },
122 { .name = "chunk_root", .type = TYPE_U64 },
123 { .name = "chunk_root_generation", .type = TYPE_U64 },
124 { .name = "cache_generation", .type = TYPE_U64 },
125 { .name = "uuid_tree_generation", .type = TYPE_U64 },
128 #define MOD_FIELD(fname, set, val) \
129 else if (strcmp(name, #fname) == 0) { \
131 printf("SET: %s %llu (0x%llx)\n", #fname, \
132 (unsigned long long)*val, (unsigned long long)*val); \
133 sb->fname = cpu_to_le64(*val); \
135 *val = le64_to_cpu(sb->fname); \
136 printf("GET: %s %llu (0x%llx)\n", #fname, \
137 (unsigned long long)*val, (unsigned long long)*val); \
141 static void mod_field_by_name(struct btrfs_super_block *sb, int set, const char *name,
145 MOD_FIELD(total_bytes, set, val)
146 MOD_FIELD(root, set, val)
147 MOD_FIELD(generation, set, val)
148 MOD_FIELD(chunk_root, set, val)
149 MOD_FIELD(chunk_root_generation, set, val)
150 MOD_FIELD(cache_generation, set, val)
151 MOD_FIELD(uuid_tree_generation, set, val)
153 printf("ERROR: unhandled field: %s\n", name);
158 static int sb_edit(struct btrfs_super_block *sb, struct fspec *fsp)
163 mod_field_by_name(sb, 0, fsp->name, &val);
165 case OP_GET: newval = val; break;
166 case OP_SET: newval = fsp->value; break;
167 case OP_ADD: newval = val + fsp->value; break;
168 case OP_SUB: newval = val - fsp->value; break;
169 case OP_XOR: newval = val ^ fsp->value; break;
170 case OP_NAND: newval = val & (~fsp->value); break;
171 case OP_BSWAP: newval = bswap_64(val); break;
172 default: printf("ERROR: unhandled operation: %d\n", fsp->fop); exit(1);
175 mod_field_by_name(sb, 1, fsp->name, &newval);
180 static int is_known_field(const char *f)
184 for (i = 0; i < ARRAY_SIZE(known_fields); i++)
185 if (strcmp(f, known_fields[i].name) == 0)
190 static int arg_to_op_value(const char *arg, enum field_op *op, u64 *val)
195 case '?': *op = OP_GET; *val = 0; break;
196 case '=': *op = OP_SET; *val = arg_strtou64(arg + 1); break;
197 case '+': *op = OP_ADD; *val = arg_strtou64(arg + 1); break;
198 case '-': *op = OP_SUB; *val = arg_strtou64(arg + 1); break;
199 case '^': *op = OP_XOR; *val = arg_strtou64(arg + 1); break;
200 case '~': *op = OP_NAND; *val = arg_strtou64(arg + 1); break;
201 case '@': *op = OP_BSWAP; *val = arg_strtou64(arg + 1); break;
203 printf("ERROR: unknown op: %c\n", arg[0]);
210 int main(int argc, char **argv) {
214 struct btrfs_header *hdr;
215 struct btrfs_super_block *sb;
217 struct fspec spec[128];
221 memset(spec, 0, sizeof(spec));
223 printf("Usage: %s image [fieldspec...]\n", argv[0]);
226 fd = open(argv[1], O_RDWR | O_EXCL);
232 /* verify superblock */
233 csum_size = btrfs_csum_sizes[BTRFS_CSUM_TYPE_CRC32];
234 off = BTRFS_SUPER_INFO_OFFSET;
236 ret = pread(fd, buf, BLOCKSIZE, off);
238 printf("pread error %d at offset %llu\n",
239 ret, (unsigned long long)off);
242 if (ret != BLOCKSIZE) {
243 printf("pread error at offset %llu: read only %d bytes\n",
244 (unsigned long long)off, ret);
247 hdr = (struct btrfs_header *)buf;
248 /* verify checksum */
249 if (!check_csum_superblock(&hdr->csum)) {
250 printf("super block checksum does not match at offset %llu, will be corrected\n",
251 (unsigned long long)off);
253 printf("super block checksum is ok\n");
255 sb = (struct btrfs_super_block *)buf;
259 for (i = 2; i < argc; i++) {
263 printf("ERROR: bad argument count\n");
268 if (!is_known_field(argv[i])) {
269 printf("ERROR: unknown filed: %s\n", argv[i]);
275 f->name = strdup(argv[i]);
277 if (arg_to_op_value(argv[i], &f->fop, &f->value)) {
283 for (i = 0; i < specidx; i++) {
284 sb_edit(sb, &spec[i]);
289 printf("Update csum\n");
290 update_block_csum(buf, 1);
291 ret = pwrite(fd, buf, BLOCKSIZE, off);
293 printf("pwrite error %d at offset %llu\n",
294 ret, (unsigned long long)off);
297 if (ret != BLOCKSIZE) {
298 printf("pwrite error at offset %llu: written only %d bytes\n",
299 (unsigned long long)off, ret);
304 printf("Nothing changed\n");