2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <ublock/ublock.h>
30 static struct ublock_ctx *ub;
31 static int ums_lun = 0;
33 static int fs_import(struct fs *fs,
34 uint16_t cluster_size, offset_t data_size,
35 offset_t *total_size_out)
39 ret = fs_init(fs, cluster_size, data_size, total_size_out);
43 ret = import_tree(fs, ".");
52 static int read_callback(char *buf, uint64_t length, uint64_t offset)
57 result = fs_read(&fs, buf, offset, length);
58 if (result == SKY_IS_FALLING) {
59 WARN("underlying filesystem has been modified; stopping.\n");
63 return result ? -EINVAL : 0;
66 static int write_callback(const char *buf, uint64_t length, uint64_t offset)
68 DEBUG("writing to (%llu, %llu): we are read-only\n", offset, length);
73 static struct ublock_ops ops = {
74 .read = &read_callback,
75 .write = &write_callback
80 static int set_ums_file(int index)
82 char filename[PATH_MAX];
85 sprintf(filename, "/sys/devices/platform/usb_mass_storage/lun%d/file",
87 file = fopen(filename, "w");
89 WARN("setting USB mass storage file: fopen(%s) failed: %s\n",
90 filename, strerror(errno));
94 WARN("writing '/dev/block/ublock%d' to %s.\n", index, filename);
96 fprintf(file, "/dev/block/ublock%d", index);
103 static int clear_ums_file(void)
105 char filename[PATH_MAX];
108 sprintf(filename, "/sys/devices/platform/usb_mass_storage/lun%d/file",
110 file = fopen(filename, "w");
112 WARN("clearing USB mass storage file: fopen(%s) failed: %s\n",
113 filename, strerror(errno));
125 static void cleanup(void)
127 WARN("cleanup: clearing USB mass storage file\n");
129 WARN("cleanup: destroying block device\n");
133 static void signal_handler(int sig)
135 WARN("received signal %d\n", sig);
140 static int normal_exit = 0;
142 static void atexit_handler(void)
150 int main(int argc, char *argv[]) {
157 signal(SIGINT, &signal_handler);
158 signal(SIGTERM, &signal_handler);
159 atexit(&atexit_handler);
162 DIE("Usage: fatblock <path> <size in MB>\n");
167 INFO("fatblock: importing filesystem from %s (%d MB)\n", path, mb);
171 DIE("fatblock: chdir(%s) failed: %s; aborting\n", path, strerror(errno));
173 ret = fs_import(&fs, 32768, 1048576LL * mb, &total_size);
175 DIE("fatblock: couldn't import filesystem; aborting\n");
177 INFO("fatblock: filesystem imported (%llu bytes)\n", total_size);
179 ret = ublock_init(&ub, &ops, total_size);
181 DIE("fatblock: couldn't create block device; aborting\n");
182 index = ublock_index(ub);
184 DIE("fatblock: invalid ublock index %d; aborting\n", index);
186 INFO("fatblock: block device ublock%d created\n", index);
189 INFO("fatblock: entering main loop\n");
192 INFO("fatblock: destroying block device\n");
198 INFO("fatblock: goodbye!\n");