diff options
author | Elliott Hughes <enh@google.com> | 2013-11-22 16:19:56 -0800 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2013-11-22 16:19:56 -0800 |
commit | 68004b775beb23fd954af63c08da232bf0835503 (patch) | |
tree | da2b4a593f76a450324af1de3ec0c00a5a4853f4 | |
parent | 67a05c0d165d779126a5fa8831cd95a0bf2fd1f2 (diff) | |
download | extras-68004b775beb23fd954af63c08da232bf0835503.tar.gz |
Remove fatblock and libublock.
The kernel support for these no longer exists.
Change-Id: I33a613e0185f03486a92b569c7d277e443fd1d48
-rw-r--r-- | fatblock/Android.mk | 23 | ||||
-rw-r--r-- | fatblock/MODULE_LICENSE_APACHE2 | 0 | ||||
-rw-r--r-- | fatblock/fat.c | 48 | ||||
-rw-r--r-- | fatblock/fat.h | 124 | ||||
-rw-r--r-- | fatblock/fatblock.c | 200 | ||||
-rw-r--r-- | fatblock/fatblock.h | 73 | ||||
-rw-r--r-- | fatblock/fdpool.c | 145 | ||||
-rw-r--r-- | fatblock/fdpool.h | 30 | ||||
-rw-r--r-- | fatblock/fs.c | 261 | ||||
-rw-r--r-- | fatblock/fs.h | 52 | ||||
-rw-r--r-- | fatblock/import.c | 365 | ||||
-rw-r--r-- | fatblock/read.c | 179 | ||||
-rw-r--r-- | fatblock/utils.c | 42 | ||||
-rw-r--r-- | fatblock/utils.h | 34 | ||||
-rw-r--r-- | libublock/Android.mk | 23 | ||||
-rw-r--r-- | libublock/MODULE_LICENSE_APACHE2 | 0 | ||||
-rw-r--r-- | libublock/NOTICE | 190 | ||||
-rw-r--r-- | libublock/include/ublock/ublock.h | 72 | ||||
-rw-r--r-- | libublock/ublock.c | 430 |
19 files changed, 0 insertions, 2291 deletions
diff --git a/fatblock/Android.mk b/fatblock/Android.mk deleted file mode 100644 index fb8d7bbe..00000000 --- a/fatblock/Android.mk +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright (C) 2010 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) - -LOCAL_MODULE := fatblock -LOCAL_MODULE_TAGS := optional -LOCAL_SRC_FILES := fat.c fatblock.c fs.c import.c read.c utils.c fdpool.c -LOCAL_C_INCLUDES := system/extras/libublock/include -LOCAL_SHARED_LIBRARIES := libublock -include $(BUILD_EXECUTABLE) diff --git a/fatblock/MODULE_LICENSE_APACHE2 b/fatblock/MODULE_LICENSE_APACHE2 deleted file mode 100644 index e69de29b..00000000 --- a/fatblock/MODULE_LICENSE_APACHE2 +++ /dev/null diff --git a/fatblock/fat.c b/fatblock/fat.c deleted file mode 100644 index c549bc1b..00000000 --- a/fatblock/fat.c +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <assert.h> -#include <string.h> -#include <endian.h> - -#include "fat.h" - -const char FAT_BOOT_SIG[] = { 0x55, 0xAA }; -const char FAT_INFO_SIG1[4] = { 'R', 'R', 'a', 'A' }; -const char FAT_INFO_SIG2[4] = { 'r', 'r', 'A', 'a' }; - -void fat_dirent_set_first_cluster(struct fat_dirent *de, cluster_t cluster) -{ - assert(de); - - de->first_cluster_hi = htole16((cluster >> 16) & 0xffff); - de->first_cluster_lo = htole16((cluster >> 0) & 0xffff); -} - -void fat_dirent_set(struct fat_dirent *de, - char *name, uint8_t attr, - cluster_t first_cluster, uint32_t size) -{ - assert(de); - assert(name); - - memset(de, 0, sizeof(*de)); - - memcpy(de->name, name, 11); - de->attr = attr; - fat_dirent_set_first_cluster(de, first_cluster); - de->size = htole32(size); -} diff --git a/fatblock/fat.h b/fatblock/fat.h deleted file mode 100644 index 64c3a513..00000000 --- a/fatblock/fat.h +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FAT_H -#define FAT_H - -#include <stdint.h> - -#include "fatblock.h" - -typedef uint32_t cluster_t; -typedef uint64_t sector_t; -typedef cluster_t fat_entry_t; - -struct fat_boot_sector { - uint8_t jump[3]; - char name[8]; - uint16_t sector_size; - uint8_t sectors_per_cluster; - uint16_t reserved_sectors; - uint8_t fats; - uint16_t rootdir_size; - uint16_t sectors16; - uint8_t media_desc; - uint16_t fat_sectors16; - uint16_t sectors_per_track; - uint16_t heads; - uint32_t hidden_sectors; - uint32_t sectors32; - uint32_t fat_sectors32; - uint16_t fat_flags; - uint16_t version; - cluster_t rootdir_start; - uint16_t fs_info_sector; - uint16_t backup_boot_sector; - uint8_t reserved1[12]; - uint8_t phys_drive; - uint8_t reserved2; - uint8_t ext_boot_sig; - uint32_t serial; - char vol_label[11]; - char type[8]; - char boot_code[420]; - uint8_t boot_sig[2]; -} __attribute__((__packed__)); - -#define FAT_MEDIA_DESC_FIXED 0xF8 - -#define FAT_PHYS_DRIVE_REMOVABLE 0x00 -#define FAT_PHYS_DRIVE_FIXED 0x80 - -#define FAT_EXT_BOOT_SIG 0x29 - -extern const char FAT_BOOT_SIG[2]; - -extern const char FAT_INFO_SIG1[4]; -extern const char FAT_INFO_SIG2[4]; -#define FAT_INFO_SIG3 FAT_BOOT_SIG - -struct fat_info_sector { - char info_sig1[4]; - char reserved1[480]; - char info_sig2[4]; - cluster_t free_clusters; - cluster_t last_cluster; - char reserved2[14]; - char info_sig3[2]; -} __attribute__((__packed__)); - -struct fat_bootinfo { - struct fat_boot_sector boot; - struct fat_info_sector info; -} __attribute__((__packed__)); - -struct fat_dirent { - char name[11]; - uint8_t attr; - uint8_t reserved; - uint8_t ctime_ms; - uint16_t ctime; - uint16_t cdate; - uint16_t adate; - uint16_t first_cluster_hi; - uint16_t mtime; - uint16_t mdate; - uint16_t first_cluster_lo; - uint32_t size; -} __attribute__((__packed__)); - -#define FAT_ATTR_READONLY 0x01 -#define FAT_ATTR_HIDDEN 0x02 -#define FAT_ATTR_SYSTEM 0x04 -#define FAT_ATTR_VOLLABEL 0x08 -#define FAT_ATTR_SUBDIR 0x10 -#define FAT_ATTR_ARCHIVE 0x20 -#define FAT_ATTR_DEVICE 0x40 - -#define FAT_ENTRY_FREE 0x00000000 -#define FAT_ENTRY_BAD 0x0FFFFFF7 -#define FAT_ENTRY_EOC 0x0FFFFFF8 - -#define FAT_SECTOR_SIZE 512 -#define FAT_CLUSTER_ZERO 2 -#define FAT_ENTRIES_PER_SECTOR ((SECTOR_SIZE) / (sizeof(fat_entry_t))) - -void fat_dirent_set_first_cluster(struct fat_dirent *de, cluster_t cluster); -void fat_dirent_set(struct fat_dirent *de, - char *name, uint8_t attr, - cluster_t first_cluster, uint32_t size); - -#endif diff --git a/fatblock/fatblock.c b/fatblock/fatblock.c deleted file mode 100644 index 244eefa4..00000000 --- a/fatblock/fatblock.c +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <assert.h> -#include <errno.h> -#include <stdint.h> -#include <stdio.h> -#include <unistd.h> - -#include <ublock/ublock.h> - -#include "fatblock.h" -#include "fs.h" -#include "utils.h" - -static struct fs fs; -static struct ublock_ctx *ub; -static int ums_lun = 0; - -static int fs_import(struct fs *fs, - uint16_t cluster_size, offset_t data_size, - offset_t *total_size_out) -{ - int ret; - - ret = fs_init(fs, cluster_size, data_size, total_size_out); - if (ret) - return ret; - - ret = import_tree(fs, "."); - if (ret) - return ret; - - return 0; -} - - - -static int read_callback(char *buf, uint64_t length, uint64_t offset) -{ - int result; - int i; - - result = fs_read(&fs, buf, offset, length); - if (result == SKY_IS_FALLING) { - WARN("underlying filesystem has been modified; stopping.\n"); - ublock_stop(ub); - } - - return result ? -EINVAL : 0; -} - -static int write_callback(const char *buf, uint64_t length, uint64_t offset) -{ - DEBUG("writing to (%llu, %llu): we are read-only\n", offset, length); - - return -EINVAL; -} - -static struct ublock_ops ops = { - .read = &read_callback, - .write = &write_callback -}; - - - -static int set_ums_file(int index) -{ - char filename[PATH_MAX]; - FILE *file; - - sprintf(filename, "/sys/devices/platform/usb_mass_storage/lun%d/file", - ums_lun); - file = fopen(filename, "w"); - if (!file) { - WARN("setting USB mass storage file: fopen(%s) failed: %s\n", - filename, strerror(errno)); - return -1; - } - - WARN("writing '/dev/block/ublock%d' to %s.\n", index, filename); - - fprintf(file, "/dev/block/ublock%d", index); - - fclose(file); - - return 0; -} - -static int clear_ums_file(void) -{ - char filename[PATH_MAX]; - FILE *file; - - sprintf(filename, "/sys/devices/platform/usb_mass_storage/lun%d/file", - ums_lun); - file = fopen(filename, "w"); - if (!file) { - WARN("clearing USB mass storage file: fopen(%s) failed: %s\n", - filename, strerror(errno)); - return -1; - } - - fclose(file); - - return 0; -} - - - - -static void cleanup(void) -{ - WARN("cleanup: clearing USB mass storage file\n"); - clear_ums_file(); - WARN("cleanup: destroying block device\n"); - ublock_destroy(ub); -} - -static void signal_handler(int sig) -{ - WARN("received signal %d\n", sig); - cleanup(); - exit(0); -} - -static int normal_exit = 0; - -static void atexit_handler(void) -{ - if (normal_exit) - return; - - cleanup(); -} - -int main(int argc, char *argv[]) { - char *path; - int mb; - offset_t total_size; - int index; - int ret; - - signal(SIGINT, &signal_handler); - signal(SIGTERM, &signal_handler); - atexit(&atexit_handler); - - if (argc != 3) - DIE("Usage: fatblock <path> <size in MB>\n"); - - path = argv[1]; - mb = atoi(argv[2]); - - INFO("fatblock: importing filesystem from %s (%d MB)\n", path, mb); - - ret = chdir(path); - if (ret < 0) - DIE("fatblock: chdir(%s) failed: %s; aborting\n", path, strerror(errno)); - - ret = fs_import(&fs, 32768, 1048576LL * mb, &total_size); - if (ret) - DIE("fatblock: couldn't import filesystem; aborting\n"); - - INFO("fatblock: filesystem imported (%llu bytes)\n", total_size); - - ret = ublock_init(&ub, &ops, total_size); - if (ret) - DIE("fatblock: couldn't create block device; aborting\n"); - index = ublock_index(ub); - if (index < 0) - DIE("fatblock: invalid ublock index %d; aborting\n", index); - - INFO("fatblock: block device ublock%d created\n", index); - set_ums_file(index); - - INFO("fatblock: entering main loop\n"); - ublock_run(ub); - - INFO("fatblock: destroying block device\n"); - clear_ums_file(); - ublock_destroy(ub); - - normal_exit = 1; - - INFO("fatblock: goodbye!\n"); - return 0; -} diff --git a/fatblock/fatblock.h b/fatblock/fatblock.h deleted file mode 100644 index 8ee4c8d8..00000000 --- a/fatblock/fatblock.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FATBLOCK_H -#define FATBLOCK_H - -#include <stdint.h> -#include <sys/types.h> - -#include "fdpool.h" - -typedef uint64_t offset_t; - -typedef enum { - EXTENT_TYPE_BOOT, - EXTENT_TYPE_INFO, - EXTENT_TYPE_FAT, - EXTENT_TYPE_FILE, - EXTENT_TYPE_DIR -} extent_type; - -struct extent { - offset_t start; - offset_t len; - extent_type type; - - struct extent *next; -}; - -struct file { - struct extent extent; - - char *path; - uint32_t size; - - dev_t dev; - ino_t ino; - time_t mtime; - - struct pooled_fd pfd; -}; - -struct dir { - struct extent extent; - - char *path; - uint32_t size; - - struct fat_dirent *entries; -}; - -struct fs; - -int import_tree(struct fs *fs, char *path); -int fs_read(struct fs *fs, char *buf, offset_t start, offset_t len); - -#define MALLOC_FAIL (-41) /* memory allocation failed somewhere. */ -#define SKY_IS_FALLING (-42) /* One of the files changed out from under us. */ - -#endif diff --git a/fatblock/fdpool.c b/fatblock/fdpool.c deleted file mode 100644 index 92df4a1a..00000000 --- a/fatblock/fdpool.c +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <assert.h> -#include <errno.h> -#include <fcntl.h> -#include <unistd.h> -#include <utils.h> - -#include "fdpool.h" - -#define INVALID_FD (-1) -#define FDPOOL_SIZE 4 - -static struct pooled_fd fdpool_head = { - .fd = INVALID_FD, - .prev = &fdpool_head, - .next = &fdpool_head -}; -static int fdpool_count = 0; - -static void fdpool_insert_head(struct pooled_fd *node) -{ - struct pooled_fd *prev = &fdpool_head; - struct pooled_fd *next = prev->next; - - assert(node); - - prev->next = node; - node->prev = prev; - node->next = next; - next->prev = node; - - fdpool_count++; -} - -static void fdpool_remove(struct pooled_fd *node) -{ - struct pooled_fd *prev = node->prev; - struct pooled_fd *next = node->next; - - assert(prev); - assert(next); - - prev->next = next; - next->prev = prev; - - fdpool_count--; -} - -static struct pooled_fd *fdpool_remove_tail(void) -{ - struct pooled_fd *tail = fdpool_head.prev; - - assert(tail != &fdpool_head); - - fdpool_remove(tail); - - return tail; -} - -static void fdpool_clear(struct pooled_fd *pfd) -{ - assert(pfd); - - pfd->fd = INVALID_FD; - pfd->prev = pfd->next = NULL; -} - -static void fdpool_unpool(struct pooled_fd *pfd) -{ - close(pfd->fd); - fdpool_clear(pfd); -} - -static void fdpool_evict(void) -{ - struct pooled_fd *tail; - - tail = fdpool_remove_tail(); - fdpool_unpool(tail); -} - -static void fdpool_pool(struct pooled_fd *pfd, int fd) -{ - if (fdpool_count >= FDPOOL_SIZE) - fdpool_evict(); - - fdpool_insert_head(pfd); - pfd->fd = fd; -} - -static void fdpool_touch(struct pooled_fd *pfd) -{ - fdpool_remove(pfd); - fdpool_insert_head(pfd); -} - - - -void fdpool_init(struct pooled_fd *pfd) -{ - fdpool_clear(pfd); -} - -int fdpool_open(struct pooled_fd *pfd, const char *pathname, int flags) -{ - int open_errno; - int fd; - - if (pfd->fd != INVALID_FD) { - fdpool_touch(pfd); - return pfd->fd; - } - - fd = open(pathname, flags); - open_errno = errno; - - if (fd >= 0) { - fdpool_pool(pfd, fd); - } - - errno = open_errno; - return fd; -} - -void fdpool_close(struct pooled_fd *pfd) -{ - assert(pfd); - - fdpool_unpool(pfd); -} diff --git a/fatblock/fdpool.h b/fatblock/fdpool.h deleted file mode 100644 index 85c7af36..00000000 --- a/fatblock/fdpool.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FDPOOL_H -#define FDPOOL_H - -struct pooled_fd { - struct pooled_fd *prev; - struct pooled_fd *next; - int fd; -}; - -void fdpool_init(struct pooled_fd *pfd); -int fdpool_open(struct pooled_fd *pfd, const char *pathname, int flags); -void fdpool_close(struct pooled_fd *pfd); - -#endif diff --git a/fatblock/fs.c b/fatblock/fs.c deleted file mode 100644 index fb19a705..00000000 --- a/fatblock/fs.c +++ /dev/null @@ -1,261 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <assert.h> -#include <string.h> -#include <endian.h> - -#include "fatblock.h" -#include "fat.h" -#include "fs.h" -#include "utils.h" - -#define DEFAULT_SECTOR_SIZE 512 - -static void fs_add_extent(struct fs *fs, struct extent *extent, - offset_t start, offset_t len, int type) -{ - assert(fs); - assert(extent); - - extent->start = start; - extent->len = len; - extent->type = type; - - extent->next = fs->extents; - fs->extents = extent; -} - -struct extent *fs_find_extent(struct fs *fs, offset_t start, offset_t len, - struct extent *last, - offset_t *r_start_out, - offset_t *e_start_out, - offset_t *len_out) -{ - struct extent *e; - offset_t end; - offset_t e_start, e_end, e_len, e_rel_start, r_rel_start, rel_len; - - assert(fs); - - end = start + len; - - e = last ? last->next : fs->extents; - for (; e; e = e->next) { - e_start = e->start; - e_len = e->len; - e_end = e_start + e_len; - - if (start >= e_end) - continue; - - if (end <= e_start) - continue; - - if (e_start <= start) { - r_rel_start = 0; - e_rel_start = start - e_start; - if (end <= e_end) - rel_len = len; - else - rel_len = e_end - start; - } else { - e_rel_start = 0; - r_rel_start = e_start - start; - if (e_end <= end) - rel_len = e_len; - else - rel_len = end - e_start; - } - - assert(e_rel_start < e_len); - assert(e_rel_start + rel_len <= e_len); - assert(r_rel_start < len); - assert(r_rel_start + rel_len <= len); - - if (r_start_out) - *r_start_out = r_rel_start; - if (e_start_out) - *e_start_out = e_rel_start; - if (len_out) - *len_out = rel_len; - - return e; - } - - return NULL; -} - -static void fs_set_fat(struct fs *fs, cluster_t cluster, fat_entry_t entry) -{ - assert(fs); - - fs->fat[cluster] = htole32(entry); -} - -int fs_alloc_extent(struct fs *fs, struct extent *extent, - offset_t len, int type, cluster_t *first_cluster_out) -{ - assert(fs); - assert(extent); - - cluster_t clusters_needed, start; - cluster_t i; - - if (len == 0) { - extent->start = 0; - extent->len = 0; - extent->type = type; - *first_cluster_out = 0; - return 0; - } - - clusters_needed = (len + fs->cluster_size - 1) / fs->cluster_size; - - /* Check for adequate space. */ - if (fs->next_cluster + clusters_needed > fs->num_clusters) { - WARN("allocating extent: filesystem is full!\n"); - return -1; - } - - /* Allocate clusters. */ - start = fs->next_cluster; - fs->next_cluster += clusters_needed; - - /* Update FAT. */ - for (i = 0; i < clusters_needed - 1; i++) { - fs_set_fat(fs, start + i, start + i + 1); - } - fs_set_fat(fs, start + clusters_needed - 1, FAT_ENTRY_EOC); - - *first_cluster_out = start; - - fs_add_extent(fs, - extent, - fs->data_offset + (offset_t)(start - FAT_CLUSTER_ZERO) - * fs->cluster_size, - (offset_t)clusters_needed * fs->cluster_size, - type); - - return 0; -} - -int fs_init(struct fs *fs, uint16_t cluster_size, offset_t data_size, - offset_t *total_size_out) -{ - uint16_t sector_size; - cluster_t data_clusters; - sector_t reserved_sectors, fat_sectors, data_sectors, total_sectors; - sector_t sectors_per_cluster; - int fat_entries_per_sector; - fat_entry_t *fat; - struct fat_boot_sector *bs; - struct fat_info_sector *is; - - assert(fs); - - sector_size = DEFAULT_SECTOR_SIZE; - fs->cluster_size = cluster_size; - - sectors_per_cluster = cluster_size / DEFAULT_SECTOR_SIZE; - fat_entries_per_sector = sector_size / sizeof(fat_entry_t); - - data_clusters = (data_size + cluster_size - 1) / cluster_size; - data_sectors = data_clusters * sectors_per_cluster; - fat_sectors = ((data_clusters + 2) + fat_entries_per_sector - 1) - / fat_entries_per_sector; - reserved_sectors = 3; - total_sectors = reserved_sectors + fat_sectors + data_sectors; - - memset(&fs->boot, 0, sizeof(fs->boot)); - bs = &fs->boot; - - strpadcpy(bs->name, "FATBLOCK", ' ', sizeof(bs->name)); - bs->sector_size = htole16(sector_size); - bs->sectors_per_cluster = sectors_per_cluster; - bs->reserved_sectors = htole16(reserved_sectors); - bs->fats = 1; - bs->media_desc = FAT_MEDIA_DESC_FIXED; - /* TODO: Calculate geometry? */ - bs->sectors_per_track = htole16(42); - bs->heads = htole16(42); - bs->sectors32 = htole32(total_sectors); - bs->fat_sectors32 = htole32(fat_sectors); - /* bs->rootdir_start will be set later. */ - bs->fs_info_sector = htole16(1); - bs->backup_boot_sector = htole16(2); - bs->phys_drive = FAT_PHYS_DRIVE_REMOVABLE; - bs->ext_boot_sig = FAT_EXT_BOOT_SIG; - bs->serial = 0x42424242; - strpadcpy(bs->vol_label, "FATBLOCK", ' ', sizeof(bs->vol_label)); - strpadcpy(bs->type, "FAT32", ' ', sizeof(bs->type)); - memcpy(bs->boot_sig, FAT_BOOT_SIG, sizeof(bs->boot_sig)); - - memset(&fs->info, 0, sizeof(fs->info)); - is = &fs->info; - - memcpy(is->info_sig1, FAT_INFO_SIG1, sizeof(is->info_sig1)); - memcpy(is->info_sig2, FAT_INFO_SIG2, sizeof(is->info_sig2)); - is->free_clusters = htole32(-1); - is->last_cluster = htole32(FAT_CLUSTER_ZERO); - memcpy(is->info_sig3, FAT_INFO_SIG3, sizeof(is->info_sig3)); - - fs->num_clusters = FAT_CLUSTER_ZERO + data_clusters; - fs->next_cluster = FAT_CLUSTER_ZERO; - - fs->fat_size = fat_sectors * sector_size; - fs->fat = malloc(fs->fat_size); - if (!fs->fat) { - WARN("initializing filesystem: couldn't allocate FAT extent: " - "out of memory\n"); - return MALLOC_FAIL; - } - memset(fs->fat, 0, fs->fat_size); - - fs->data_offset = (reserved_sectors + fat_sectors) * sector_size; - - fs->extents = NULL; - fs_add_extent(fs, &fs->boot_extent, - 0, sector_size, - EXTENT_TYPE_BOOT); - fs_add_extent(fs, &fs->info_extent, - sector_size, sector_size, - EXTENT_TYPE_INFO); - fs_add_extent(fs, &fs->backup_boot_extent, - 2 * sector_size, sector_size, - EXTENT_TYPE_BOOT); - fs_add_extent(fs, &fs->fat_extent, - reserved_sectors * sector_size, fs->fat_size, - EXTENT_TYPE_FAT); - - *total_size_out = (offset_t)total_sectors * sector_size; - - return 0; -} - -void fs_set_rootdir_start(struct fs *fs, cluster_t rootdir_start) -{ - assert(fs); - - fs->boot.rootdir_start = htole32(rootdir_start); -} - -void fs_update_free_clusters(struct fs *fs) -{ - assert(fs); - - fs->info.free_clusters = htole32(fs->num_clusters - fs->next_cluster); -} diff --git a/fatblock/fs.h b/fatblock/fs.h deleted file mode 100644 index 52f3383d..00000000 --- a/fatblock/fs.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FS_H -#define FS_H - -#include "fatblock.h" -#include "fat.h" - -struct fs { - uint16_t cluster_size; - - cluster_t num_clusters; - cluster_t next_cluster; - struct extent *extents; - - struct fat_boot_sector boot; - struct extent boot_extent; - struct extent backup_boot_extent; - - struct fat_info_sector info; - struct extent info_extent; - - struct extent fat_extent; - fat_entry_t *fat; - offset_t fat_size; - - offset_t data_offset; -}; - -int fs_alloc_extent(struct fs *fs, struct extent *extent, - offset_t len, int type, cluster_t *first_cluster_out); -struct extent *fs_find_extent(struct fs *fs, offset_t start, offset_t len, struct extent *last, - offset_t *r_start_out, offset_t *e_start_out, offset_t *len_out); -int fs_init(struct fs *fs, uint16_t cluster_size, offset_t data_size, offset_t *total_size_out); -void fs_set_rootdir_start(struct fs *fs, cluster_t rootdir_start); -void fs_update_free_clusters(struct fs *fs); - -#endif diff --git a/fatblock/import.c b/fatblock/import.c deleted file mode 100644 index 80ed5b85..00000000 --- a/fatblock/import.c +++ /dev/null @@ -1,365 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <assert.h> -#include <ctype.h> -#include <dirent.h> -#include <errno.h> -#include <unistd.h> -#include <stdio.h> -#include <string.h> -#include <sys/stat.h> - -#include "fatblock.h" -#include "fat.h" -#include "fdpool.h" -#include "fs.h" -#include "utils.h" - -static inline int valid_char(int c) -{ - return (isalnum(c) || - strchr("!#$%'()-@^_`{}~", c) || - ((c >= 128) && (c < 256))); -} - -static int convert_name(char *short_name, const char *long_name) -{ - int i; - - const char *s; - const char *dot; - int c; - - dot = NULL; - - for (s = long_name; *s; s++) { - if (*s == '.') { - if (dot) { - goto short_fail; - } else { - dot = s; - } - } else if (!valid_char(*s)) { - goto short_fail; - } - } - - if (dot - long_name > 8) { - goto short_fail; - } - - if (dot && (s - (dot + 1) > 3)) { - goto short_fail; - } - - memset(short_name, ' ', 11); - - if (!dot) { - dot = s; - } - - for (i = 0; i < dot - long_name; i++) { - short_name[i] = toupper(long_name[i]); - } - - for (i = 0; i < s - dot; i++) { - short_name[8 + i] = toupper(dot[1 + i]); - } - - return 0; - -short_fail: - return 1; -} - -struct imported { - cluster_t first_cluster; - uint32_t size; - struct fat_dirent *dot_dot_dirent; -}; - -static int import_file(struct fs *fs, char *path, struct imported *out) -{ - struct stat st; - struct file *f = NULL; - char *path_copy = NULL; - int ret; - - ret = stat(path, &st); - if (ret < 0) { - WARN("importing %s: stat failed: %s\n", path, strerror(errno)); - goto fail; - } - - f = malloc(sizeof(struct file)); - if (!f) { - WARN("importing %s: couldn't allocate file struct: " - "out of memory\n", path); - ret = MALLOC_FAIL; - goto fail; - } - - path_copy = strdup(path); - if (!path_copy) { - WARN("importing %s: couldn't strdup path: out of memory\n", - path); - ret = MALLOC_FAIL; - goto fail; - } - - f->path = path_copy; - f->size = st.st_size; - f->dev = st.st_dev; - f->ino = st.st_ino; - f->mtime = st.st_mtime; - fdpool_init(&f->pfd); - - ret = fs_alloc_extent(fs, &f->extent, - f->size, EXTENT_TYPE_FILE, &out->first_cluster); - if (ret) { - WARN("importing %s: couldn't allocate data extent\n", path); - goto fail; - } - - out->size = f->size; - out->dot_dot_dirent = NULL; - - return 0; - -fail: - if (path_copy) - free(path_copy); - if (f) - free(f); - return ret; -} - -struct item { - char name[11]; - struct imported imp; - struct item *next; - int is_dir; -}; - -static struct item *free_items_head; - -static struct item *alloc_item(void) -{ - struct item *item; - - if (free_items_head) { - item = free_items_head; - free_items_head = item->next; - } else { - item = malloc(sizeof(struct item)); - /* May return NULL if item couldn't be allocated. */ - } - - return item; -} - -static void free_item(struct item *item) -{ - item->next = free_items_head; - free_items_head = item; -} - -static void free_items(struct item *head) -{ - struct item *tail; - - for (tail = head; tail->next; tail = tail->next); - - tail->next = free_items_head; - free_items_head = head; -} - -/* TODO: With some work, this can be rewritten so we don't recurse - * until all memory is allocated. */ -static int import_dir(struct fs *fs, char *path, int is_root, - struct imported *out) -{ - struct dir *d; - cluster_t my_first_cluster; - - DIR *dir; - struct dirent *de; - - char ch_path[PATH_MAX]; - struct imported *ch_imp; - cluster_t ch_first_cluster; - struct fat_dirent *ch_dirent; - - int ret; - - struct item *items; - struct item *item; - int count; - - int i; - - dir = opendir(path); - if (!dir) { - WARN("importing %s: opendir failed: %s\n", path, - strerror(errno)); - return -1; - } - - d = malloc(sizeof(struct dir)); - if (!d) { - WARN("importing %s: couldn't allocate dir struct: " - "out of memory\n", path); - closedir(dir); - return MALLOC_FAIL; - } - - d->path = strdup(path); - if (!d->path) { - WARN("importing %s: couldn't strdup path: out of memory\n", - path); - closedir(dir); - free(d); - return MALLOC_FAIL; - } - - items = NULL; - item = NULL; - count = 0; - - while ((de = readdir(dir))) { - if (de->d_name[0] == '.') { - goto skip_item; - } - - ret = snprintf(ch_path, PATH_MAX, "%s/%s", path, de->d_name); - if (ret < 0 || ret >= PATH_MAX) { - goto skip_item; - } - - item = alloc_item(); - if (!item) { - WARN("importing %s: couldn't allocate item struct: " - "out of memory\n", path); - ret = MALLOC_FAIL; - goto free_items; - } - - if (convert_name(item->name, de->d_name)) { - goto skip_item; - } - - switch (de->d_type) { - case DT_REG: - import_file(fs, ch_path, &item->imp); - item->is_dir = 0; - break; - case DT_DIR: - import_dir(fs, ch_path, 0, &item->imp); - item->is_dir = 1; - break; - default: - goto skip_item; - } - - item->next = items; - items = item; - - count++; - - item = NULL; - - continue; - -skip_item: - if (item) - free_item(item); - } - - closedir(dir); - - d->size = sizeof(struct fat_dirent) * (count + (is_root ? 0 : 2)); - ret = fs_alloc_extent(fs, &d->extent, d->size, EXTENT_TYPE_DIR, &out->first_cluster); - if (ret) { - WARN("importing %s: couldn't allocate directory table extent: " - "out of space\n", path); - goto free_items; - } - - if (is_root) - out->first_cluster = 0; - - my_first_cluster = is_root ? 0 : out->first_cluster; - - d->entries = malloc(sizeof(struct fat_dirent) * (count + (is_root ? 0 : 2))); - assert(d->entries); - for (i = count - 1; i >= 0; i--) { - item = items; - items = item->next; - - ch_dirent = &d->entries[i + (is_root ? 0 : 2)]; - - fat_dirent_set(ch_dirent, - item->name, item->is_dir ? FAT_ATTR_SUBDIR : 0, - item->imp.first_cluster, item->imp.size); - - if (item->imp.dot_dot_dirent) { - fat_dirent_set_first_cluster(item->imp.dot_dot_dirent, - my_first_cluster); - } - - free_item(item); - } - - if (!is_root) { - fat_dirent_set(&d->entries[0], - ".. ", FAT_ATTR_SUBDIR, - (cluster_t)-1, 0); - out->dot_dot_dirent = &d->entries[0]; /* will set first_cluster */ - - fat_dirent_set(&d->entries[1], - ". ", FAT_ATTR_SUBDIR, - my_first_cluster, 0); - } else { - out->dot_dot_dirent = NULL; - } - - out->size = 0; - - return 0; - -free_items: - free_items(items); - free(d->path); - free(d); - - return ret; -} - -int import_tree(struct fs *fs, char *path) -{ - struct imported imp; - int ret; - - ret = import_dir(fs, path, 0, &imp); - if (ret) - return ret; - - fs_set_rootdir_start(fs, imp.first_cluster); - fs_update_free_clusters(fs); - - return 0; -} diff --git a/fatblock/read.c b/fatblock/read.c deleted file mode 100644 index ffb1aecf..00000000 --- a/fatblock/read.c +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <assert.h> -#include <errno.h> -#include <fcntl.h> -#include <string.h> -#include <unistd.h> -#include <sys/stat.h> - -#include "fatblock.h" -#include "fs.h" -#include "utils.h" - -static int buffer_read(char *buf, offset_t buf_len, char *out, - offset_t off, offset_t len) -{ - assert(buf); - assert(out); - - if (off >= buf_len) { - memset(out, 0, len); - return 0; - } - - if (off + len > buf_len) { - memset(out + (buf_len - off), 0, len - (buf_len - off)); - len = buf_len - off; - } - - assert(off < buf_len); - assert(off + len <= buf_len); - - memcpy(out, buf + off, len); - - return 0; -} - -static int file_check_metadata(struct file *f) -{ - struct stat st; - int ret; - - assert(f); - - ret = stat(f->path, &st); - if (ret) { - WARN("checking metadata of %s: stat failed: %s\n", - f->path, strerror(errno)); - return -1; - } - - if (f->mtime != st.st_mtime) - return -1; - - return 0; -} - -static int file_read(struct file *f, char *buf, offset_t off, offset_t len) -{ - int fd; - off_t sought; - ssize_t ret; - - assert(f); - assert(buf); - - if (off >= UINT32_MAX) { - WARN("reading %s (%llu, %llu): " - "ignoring read that starts past 2^32\n", - f->path, off, len); - return 0; - } - - if (off + len > UINT32_MAX) { - WARN("reading %s (%llu, %llu): " - "truncating read that ends past 2^32\n", - f->path, off, len); - len = UINT32_MAX - off; - } - - if (file_check_metadata(f)) { - WARN("reading %s (%llu, %llu): metadata has changed\n", - f->path, off, len); - return SKY_IS_FALLING; - } - - fd = fdpool_open(&f->pfd, f->path, O_RDONLY); - if (fd < 0) { - WARN("reading %s: open failed: %s\n", f->path, strerror(errno)); - return -1; - } - - sought = lseek(fd, (off_t)len, SEEK_SET); - if (sought != (off_t)len) { - WARN("reading %s (%llu, %llu): seek failed: %s\n", - f->path, off, len, strerror(errno)); - return -1; - } - - ret = read(fd, buf, (size_t)len); - if (ret != (ssize_t)len) { - WARN("reading %s (%llu, %llu): read failed: %s\n", - f->path, off, len, strerror(errno)); - return -1; - } - - /* leave fd open; fdpool will close it if needed. */ - - return 0; -} - -static int dir_read(struct dir *d, char *buf, offset_t off, offset_t len) -{ - assert(d); - assert(buf); - - return buffer_read((char*)d->entries, d->size, buf, off, len); -} - -static int extent_read(struct fs *fs, struct extent *e, char *buf, - offset_t off, offset_t len) -{ - assert(fs); - assert(e); - assert(buf); - - switch (e->type) { - case EXTENT_TYPE_BOOT: - return buffer_read((char*)&fs->boot, sizeof(fs->boot), buf, - off, len); - case EXTENT_TYPE_INFO: - return buffer_read((char*)&fs->info, sizeof(fs->info), buf, - off, len); - case EXTENT_TYPE_FAT: - return buffer_read((char*)fs->fat, fs->fat_size, buf, - off, len); - case EXTENT_TYPE_FILE: - return file_read((struct file *)e, buf, off, len); - case EXTENT_TYPE_DIR: - return dir_read((struct dir *)e, buf, off, len); - default: - WARN("reading extent: unexpected type %d\n", e->type); - return -1; - } -} - -int fs_read(struct fs *fs, char *buf, offset_t start, offset_t len) -{ - struct extent *e = NULL; - offset_t e_start, r_start, rel_len; - int ret; - - memset(buf, 0, len); - - while ((e = fs_find_extent(fs, start, len, e, - &r_start, &e_start, &rel_len))) { - ret = extent_read(fs, e, buf + r_start, e_start, rel_len); - if (ret == SKY_IS_FALLING) - return SKY_IS_FALLING; - if (ret) - return ret; - } - - return 0; -} diff --git a/fatblock/utils.c b/fatblock/utils.c deleted file mode 100644 index 20fa83bc..00000000 --- a/fatblock/utils.c +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#include "utils.h" - -void strpadcpy(char *d, const char *s, char p, size_t l) -{ - while (*s && l-- > 0) - *d++ = *s++; - - while (l-- > 0) - *d++ = ' '; -} - -void warn(char *msg) -{ - fprintf(stderr, "%s", msg); -} - -void die(char *msg) -{ - fprintf(stderr, "%s", msg); - exit(EXIT_FAILURE); -} diff --git a/fatblock/utils.h b/fatblock/utils.h deleted file mode 100644 index 7fe4b882..00000000 --- a/fatblock/utils.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef UTILS_H -#define UTILS_H - -#include <stdint.h> -#include <stdio.h> -#include <stdlib.h> - -#define EPRINT(...) fprintf(stderr, __VA_ARGS__) -#define DIE(...) do { EPRINT(__VA_ARGS__); exit(EXIT_FAILURE); } while (0) -#define WARN(...) EPRINT(__VA_ARGS__) -#define INFO(...) EPRINT(__VA_ARGS__) -#define DEBUG(...) EPRINT(__VA_ARGS__) - -void strpadcpy(char *d, const char *s, char p, size_t l); -void warn(char *msg); -void die(char *msg); - -#endif diff --git a/libublock/Android.mk b/libublock/Android.mk deleted file mode 100644 index 690c5e8e..00000000 --- a/libublock/Android.mk +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright (C) 2010 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) - -LOCAL_MODULE := libublock -LOCAL_MODULE_TAGS := optional -LOCAL_SRC_FILES := ublock.c -LOCAL_C_INCLUDES := $(LOCAL_PATH)/include - -include $(BUILD_SHARED_LIBRARY) diff --git a/libublock/MODULE_LICENSE_APACHE2 b/libublock/MODULE_LICENSE_APACHE2 deleted file mode 100644 index e69de29b..00000000 --- a/libublock/MODULE_LICENSE_APACHE2 +++ /dev/null diff --git a/libublock/NOTICE b/libublock/NOTICE deleted file mode 100644 index c5b1efa7..00000000 --- a/libublock/NOTICE +++ /dev/null @@ -1,190 +0,0 @@ - - Copyright (c) 2005-2008, The Android Open Source Project - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - diff --git a/libublock/include/ublock/ublock.h b/libublock/include/ublock/ublock.h deleted file mode 100644 index 5b691b0e..00000000 --- a/libublock/include/ublock/ublock.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef UBLOCK_UBLOCK_H -#define UBLOCK_UBLOCK_H - -#include <stdint.h> -#include <sys/types.h> - -#define UBLOCK_VERSION 0 - -struct ublock_ctx; - -struct ublock_ops { - int (*read)(char *buf, uint64_t length, uint64_t offset); - int (*write)(const char *buf, uint64_t length, uint64_t offset); -}; - -/* - * Initializes a ublock block device. - * - * May call your read and write functions as the kernel scans partition - * tables. Will return once the kernel has finished adding the block device. - * - * Returns 0 on success, - * -ENOMEM if we ran out of memory, - * -ENOENT if the kernel appears to lack ublock support, and - * -EPROTO if the kernel did something unexpected. - */ -int ublock_init(struct ublock_ctx **ub, struct ublock_ops *ops, uint64_t size); - -/* - * Returns the index of a ublock block device. - * - * Returns -EFAULT if the context is NULL, - * -EINVAL if the context is invalid. - */ -int ublock_index(struct ublock_ctx *ub); - -/* - * Runs a loop waiting for ublock requests and calling the ops callbacks. - * - * Returns 0 if the loop was stopped by ublock_stop, - * -EPROTO if the loop was stopped by a protocol error, - */ -int ublock_run(struct ublock_ctx *ub); - -/* - * Stops ublock_run, if it is running. Should be called from one of the ops - * callbacks. - */ -void ublock_stop(struct ublock_ctx *ub); - -/* - * Destroys a ublock block device. - */ -void ublock_destroy(struct ublock_ctx *ub); - -#endif diff --git a/libublock/ublock.c b/libublock/ublock.c deleted file mode 100644 index c2627b1e..00000000 --- a/libublock/ublock.c +++ /dev/null @@ -1,430 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <assert.h> -#include <errno.h> -#include <fcntl.h> -#include <linux/ublock.h> -#include <stdint.h> -#include <stdio.h> -#include <stdlib.h> -#include <sys/types.h> -#include <ublock/ublock.h> - -#define CONTROL_FILE "/dev/ublockctl" - -struct ublock_ctx { - struct ublock_ops *ops; - - uint32_t index; - uint64_t size; - uint32_t max_buf; - - char *in_buf; - char *out_buf; - - int flags; - int fd; - int fails; -}; - -#define CTX_INITED 0x1 -#define CTX_READY 0x2 -#define CTX_RUNNING 0x4 - -#define MAX_BUF 65536 - -#define MAX_FAILURES 10 - -static inline void ublock_succeed(struct ublock_ctx *ub_ctx) -{ - assert(ub_ctx); - - ub_ctx->fails = 0; -} - -static void ublock_fail(struct ublock_ctx *ub_ctx) -{ - assert(ub_ctx); - - ub_ctx->fails++; - if (ub_ctx->fails > MAX_FAILURES) - ublock_stop(ub_ctx); -} - -static int ublock_handle_init(struct ublock_ctx *ub_ctx, - const void *in, size_t in_len, - void *out, size_t *out_len) -{ - const struct ublock_init_in *in_h; - struct ublock_init_out *out_h; - - assert(ub_ctx); - assert(in); - assert(out); - - if (in_len != sizeof(*in_h)) - return -EPROTO; - - in_h = (const struct ublock_init_in *)in; - - if (in_h->version != UBLOCK_VERSION) - return -EPROTO; - - out_h = (struct ublock_init_out *)out; - out_h->version = UBLOCK_VERSION; - out_h->size = ub_ctx->size; - if (in_h->max_buf < MAX_BUF) - ub_ctx->max_buf = in_h->max_buf; - else - ub_ctx->max_buf = MAX_BUF; - out_h->max_buf = ub_ctx->max_buf; - - *out_len = sizeof(*out_h); - - ub_ctx->index = in_h->index; - ub_ctx->flags |= CTX_INITED; - - return 0; -} - -static int ublock_handle_ready(struct ublock_ctx *ub_ctx, - const void *in, size_t in_len, - void *out, size_t *out_len) -{ - struct ublock_ready_out *out_h; - - assert(ub_ctx); - assert(in); - assert(out); - - if (in_len != sizeof(struct ublock_ready_in)) - return -EPROTO; - - *out_len = sizeof(struct ublock_ready_out); - - ub_ctx->flags |= CTX_READY; - - return 0; -} - -static int ublock_handle_read(struct ublock_ctx *ub_ctx, - const void *in, size_t in_len, - void *out, size_t *out_len) -{ - const struct ublock_read_in *in_h; - struct ublock_read_out *out_h; - char *out_buf; - - assert(ub_ctx); - assert(in); - assert(out); - - if (in_len != sizeof(*in_h)) - return -EPROTO; - - in_h = (const struct ublock_read_in *)in; - - out_h = (struct ublock_read_out *)out; - out_buf = (char *)(out_h + 1); - - out_h->status = (ub_ctx->ops->read)(out_buf, in_h->length, in_h->offset); - - if (out_h->status >= 0) - *out_len = sizeof(*out_h) + in_h->length; - else - *out_len = sizeof(*out_h); - - return 0; -} - -static int ublock_handle_write(struct ublock_ctx *ub_ctx, - const void *in, size_t in_len, - void *out, size_t *out_len) -{ - const struct ublock_write_in *in_h; - const char *in_buf; - struct ublock_write_out *out_h; - - assert(ub_ctx); - assert(in); - assert(out); - - if (in_len < sizeof(*in_h)) - return -EPROTO; - - in_h = (const struct ublock_write_in *)in; - in_buf = (const char*)(in_h + 1); - - out_h = (struct ublock_write_out *)out; - *out_len = sizeof(*out_h); - - out_h->status = (ub_ctx->ops->write)(in_buf, in_h->length, in_h->offset); - - return 0; -} - -static int ublock_handle_request(struct ublock_ctx *ub_ctx, - const void *in, size_t in_len, - void *out, size_t *out_len) -{ - const struct ublock_in_header *in_h; - const void *in_buf; - size_t in_buf_len; - - struct ublock_out_header *out_h; - void *out_buf; - size_t out_buf_len; - - int result; - int (*handle_fn)(struct ublock_ctx *, const void *, size_t, void *, size_t *); - - assert(ub_ctx); - assert(in); - assert(out); - - if (in_len < sizeof(*in_h)) - return -EPROTO; - - in_h = (const struct ublock_in_header *)in; - in_buf = in_h + 1; - in_buf_len = in_len - sizeof(*in_h); - - out_h = (struct ublock_out_header *)out; - out_buf = out_h + 1; - - switch (in_h->opcode) { - case UBLOCK_INIT_IN: - out_h->opcode = UBLOCK_INIT_OUT; - handle_fn = &ublock_handle_init; - break; - case UBLOCK_READY_IN: - out_h->opcode = UBLOCK_READY_OUT; - handle_fn = &ublock_handle_ready; - break; - case UBLOCK_READ_IN: - out_h->opcode = UBLOCK_READ_OUT; - handle_fn = &ublock_handle_read; - break; - case UBLOCK_WRITE_IN: - out_h->opcode = UBLOCK_WRITE_OUT; - handle_fn = &ublock_handle_write; - break; - default: - return -EPROTO; - } - - out_h->seq = in_h->seq; - result = (handle_fn)(ub_ctx, in_buf, in_buf_len, out_buf, &out_buf_len); - *out_len = sizeof(*out_h) + out_buf_len; - - return result; -} - -static int ublock_do_request(struct ublock_ctx *ub_ctx, - void *in_buf, size_t in_size, - void *out_buf, size_t out_size) -{ - size_t out_len; - ssize_t in_len, out_wrote; - int result; - - assert(ub_ctx); - assert(in_buf); - assert(out_buf); - - in_len = read(ub_ctx->fd, in_buf, in_size); - if (in_len < 0) - return -EPROTO; - - result = ublock_handle_request(ub_ctx, in_buf, in_len, out_buf, &out_len); - - assert(out_len <= out_size); - - out_wrote = write(ub_ctx->fd, out_buf, out_len); - - if (out_wrote < out_len) - return -EPROTO; - - if (result) - ublock_fail(ub_ctx); - else - ublock_succeed(ub_ctx); - - return result; -} - -#define EXIT_READY 1 -#define EXIT_STOPPED 2 -#define EXIT_FAIL 4 - -static int ublock_loop(struct ublock_ctx *ub_ctx, int exit_cond) -{ - size_t in_len, out_len; - int result; - - result = 0; - while (((exit_cond & EXIT_READY) && (!(ub_ctx->flags & CTX_READY))) || - ((exit_cond & EXIT_STOPPED) && (ub_ctx->flags & CTX_RUNNING)) || - ((exit_cond & EXIT_FAIL) && (result))) { - result = ublock_do_request(ub_ctx, - ub_ctx->in_buf, ub_ctx->max_buf, - ub_ctx->out_buf, ub_ctx->max_buf); - if (result) - return result; - } - - return 0; -} - -int ublock_run(struct ublock_ctx *ub_ctx) -{ - if (!ub_ctx) - return -EFAULT; - - if (!(ub_ctx->flags & CTX_INITED)) - return -EINVAL; - - ub_ctx->flags |= CTX_RUNNING; - ublock_loop(ub_ctx, EXIT_STOPPED); - - return 0; -} - -void ublock_stop(struct ublock_ctx *ub_ctx) -{ - if (!ub_ctx) - return; - - if (!(ub_ctx->flags & CTX_INITED)) - return; - - ub_ctx->flags &= ~CTX_RUNNING; -} - -int ublock_index(struct ublock_ctx *ub_ctx) -{ - if (!ub_ctx) - return -EFAULT; - - if (!(ub_ctx->flags & CTX_INITED)) - return -EINVAL; - - return ub_ctx->index; -} - -static inline size_t ublock_init_buf_size(void) -{ - size_t in_size = sizeof(struct ublock_in_header) + - sizeof(struct ublock_init_in); - size_t out_size = sizeof(struct ublock_out_header) + - sizeof(struct ublock_init_out); - - return (in_size > out_size) ? in_size : out_size; -} - -int ublock_init(struct ublock_ctx **ub_ctx_out, struct ublock_ops *ops, - uint64_t dev_size) -{ - struct ublock_ctx *ub_ctx; - char *in_buf, *out_buf; - size_t size; - int result; - - if (!ub_ctx_out || !ops) - return -EFAULT; - - in_buf = out_buf = NULL; - - ub_ctx = malloc(sizeof(struct ublock_ctx)); - if (!ub_ctx) { - result = -ENOMEM; - goto error; - } - - size = ublock_init_buf_size(); - in_buf = malloc(size); - out_buf = malloc(size); - if (!(in_buf && out_buf)) { - result = -ENOMEM; - goto error; - } - - ub_ctx->ops = ops; - ub_ctx->size = dev_size; - ub_ctx->max_buf = 0; - ub_ctx->flags = 0; - - ub_ctx->fd = open(CONTROL_FILE, O_RDWR); - if (ub_ctx->fd < 0) { - result = -ENOENT; - goto error; - } - - result = ublock_do_request(ub_ctx, in_buf, size, out_buf, size); - if (result) { - result = -EPROTO; - goto error; - } - if (!ub_ctx->flags & CTX_INITED) { - result = -EPROTO; - goto error; - } - - free(in_buf); - in_buf = NULL; - free(out_buf); - out_buf = NULL; - - ub_ctx->in_buf = malloc(ub_ctx->max_buf); - ub_ctx->out_buf = malloc(ub_ctx->max_buf); - if (!(ub_ctx->in_buf && ub_ctx->out_buf)) { - result = -ENOMEM; - goto error; - } - - ublock_loop(ub_ctx, EXIT_READY); - - *ub_ctx_out = ub_ctx; - - return 0; - -error: - if (ub_ctx) { - if (ub_ctx->in_buf) - free(ub_ctx->in_buf); - if (ub_ctx->out_buf) - free(ub_ctx->out_buf); - if (ub_ctx->fd) - close(ub_ctx->fd); - free(ub_ctx); - } - if (in_buf) - free(in_buf); - if (out_buf) - free(out_buf); - - return result; -} - -void ublock_destroy(struct ublock_ctx *ub_ctx) -{ - if (!ub_ctx) - return; - - close(ub_ctx->fd); - free(ub_ctx); -} |