u64 get_file_size(int fd) { struct stat buf; int ret; u64 reserve_len = 0; s64 computed_size; ret = fstat(fd, &buf); if (ret) return 0; if (info.len < 0) reserve_len = -info.len; if (S_ISREG(buf.st_mode)) computed_size = buf.st_size - reserve_len; else if (S_ISBLK(buf.st_mode)) computed_size = get_block_device_size(fd) - reserve_len; else computed_size = 0; if (computed_size < 0) { warn("Computed filesystem size less than 0"); computed_size = 0; } return computed_size; }
int wipe_block_device(int fd) { uint64_t range[2]; int ret; uint64_t len = get_block_device_size(fd); range[0] = 0; range[1] = len; if (range[1] == 0) return 0; ret = ioctl(fd, BLKSECDISCARD, &range); if (ret < 0) { ALOGE("Something went wrong secure discarding block: %s\n", strerror(errno)); range[0] = 0; range[1] = len; ret = ioctl(fd, BLKDISCARD, &range); if (ret < 0) { ALOGE("Discard failed: %s\n", strerror(errno)); return -1; } else { ALOGE("Wipe via secure discard failed, used non-secure discard instead\n"); return 0; } } return ret; }
int flash_erase(int fd) { int64_t size; size = get_block_device_size(fd); D(DEBUG, "erase %"PRId64" data from %d\n", size, fd); return wipe_block_device(fd, size); }
int main(int argc, char *argv[]) { int secure = 0; char *devname; int fd; u64 len; struct stat statbuf; int ret; if ((argc != 2) && (argc != 3)) { usage(); } if (argc == 3) { if (!strcmp(argv[1], "-s")) { secure = 1; devname = argv[2]; } else { usage(); } } else { devname = argv[1]; } fd = open(devname, O_RDWR); if (fd < 0) { fprintf(stderr, "Cannot open device %s\n", devname); exit(1); } if (fstat(fd, &statbuf) < 0) { fprintf(stderr, "Cannot stat %s\n", devname); exit(1); } if (!S_ISBLK(statbuf.st_mode)) { fprintf(stderr, "%s is not a block device\n", devname); exit(1); } len = get_block_device_size(fd); if (! len) { fprintf(stderr, "Cannot get size of block device %s\n", devname); exit(1); } ret = wipe_block_device(fd, len, secure); close(fd); return ret; }
static jlong com_android_server_PersistentDataBlockService_getBlockDeviceSize(JNIEnv *env, jclass, jstring jpath) { ScopedUtfChars path(env, jpath); int fd = open(path.c_str(), O_RDONLY); if (fd < 0) return 0; const uint64_t size = get_block_device_size(fd); close(fd); return size; }
int get_device_info(int fd, struct device_info *info) { blkid_probe probe; struct stat stat; int ret; *info = device_info_clueless; ret = fstat(fd, &stat); if (ret < 0) { perror("fstat on target failed"); return -1; } if (S_ISREG(stat.st_mode)) { /* there is nothing more to discover for an image file */ info->type = TYPE_FILE; info->partition = 0; info->size = stat.st_size; return 0; } if (!S_ISBLK(stat.st_mode)) { /* neither regular file nor block device? not usable */ info->type = TYPE_BAD; return 0; } probe = blkid_new_probe(); if (!probe) { return -1; } if (blkid_probe_set_device(probe, fd, 0, 0)) { blkid_free_probe(probe); return -1; } get_block_device_size(info, fd); get_block_geometry(info, fd); info->sector_size = blkid_probe_get_sectorsize(probe); /* use udev information if available, fall back to blkid probing */ if (udev_fill_info(info, &stat)) blkid_fill_info(info, probe); blkid_free_probe(probe); return 0; }
uint64_t get_file_size64(int fd) { struct stat64 buf; int ret; uint64_t computed_size; ret = fstat64(fd, &buf); if (ret) return 0; if (S_ISREG(buf.st_mode)) computed_size = buf.st_size; else if (S_ISBLK(buf.st_mode)) computed_size = get_block_device_size(fd); else computed_size = 0; return computed_size; }
int get_device_info(int fd, struct device_info *info) { struct stat stat; int ret; *info = device_info_clueless; ret = fstat(fd, &stat); if (ret < 0) { perror("fstat on target failed"); return -1; } if (S_ISREG(stat.st_mode)) { /* there is nothing more to discover for an image file */ info->type = TYPE_FILE; info->partition = 0; info->size = stat.st_size; return 0; } if (!S_ISBLK(stat.st_mode)) { /* neither regular file nor block device? not usable */ info->type = TYPE_BAD; return 0; } get_block_device_size(info, fd); get_block_geometry(info, fd); get_sector_size(info, fd); /* use udev information if available */ udev_fill_info(info, &stat); return 0; }
int format_volume(const char* volume) { Volume* v = volume_for_path(volume); if (v == NULL) { LOGE("unknown volume \"%s\"\n", volume); return -1; } if (strcmp(v->fs_type, "ramdisk") == 0) { // you can't format the ramdisk. LOGE("can't format_volume \"%s\"", volume); return -1; } if (strcmp(v->mount_point, volume) != 0) { LOGE("can't give path \"%s\" to format_volume\n", volume); return -1; } if (ensure_path_unmounted(volume) != 0) { LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point); return -1; } if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) { mtd_scan_partitions(); const MtdPartition* partition = mtd_find_partition_by_name(v->blk_device); if (partition == NULL) { LOGE("format_volume: no MTD partition \"%s\"\n", v->blk_device); return -1; } MtdWriteContext *write = mtd_write_partition(partition); if (write == NULL) { LOGW("format_volume: can't open MTD \"%s\"\n", v->blk_device); return -1; } else if (mtd_erase_blocks(write, -1) == (off_t) -1) { LOGW("format_volume: can't erase MTD \"%s\"\n", v->blk_device); mtd_write_close(write); return -1; } else if (mtd_write_close(write)) { LOGW("format_volume: can't close MTD \"%s\"\n", v->blk_device); return -1; } return 0; } if (strcmp(v->fs_type, "ext4") == 0) { if (fs_mgr_is_encryptable(v) && (v->length == 0) && (v->key_loc != NULL) && !strcmp(v->key_loc, KEY_IN_FOOTER)) { if (get_block_device_size(v->blk_device, &v->length) == 0) { v->length -= CRYPT_FOOTER_OFFSET; } } int result = make_ext4fs(v->blk_device, v->length, volume, sehandle); if (result != 0) { LOGE("format_volume: make_extf4fs failed on %s\n", v->blk_device); return -1; } return 0; } #ifdef USE_UBIFS if (strcmp(v->fs_type, "ubifs") == 0) { int ret; LOGW("formating by ubiVolumeFormat"); ret = ubiVolumeFormat(v->blk_device); if (ret != 0) { LOGE("ubiVolumeFormat return error:%d", ret); return -1; } return 0; } #endif LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type); return -1; }