ext4fixup.c \
ext4_utils.c \
allocate.c \
- backed_block.c \
- output_file.c \
contents.c \
extent.c \
indirect.c \
uuid.c \
sha1.c \
- sparse_crc32.c \
wipe.c
# -- All host/targets including windows
LOCAL_MODULE := libext4_utils
LOCAL_MODULE_TAGS := optional
LOCAL_C_INCLUDES += external/zlib
+LOCAL_STATIC_LIBRARIES += libsparse
ifeq ($(HAVE_SELINUX), true)
LOCAL_C_INCLUDES += external/libselinux/include
LOCAL_SRC_FILES := make_ext4fs_main.c
LOCAL_MODULE := make_ext4fs
-LOCAL_STATIC_LIBRARIES += libext4_utils libz
+LOCAL_STATIC_LIBRARIES += libext4_utils libsparse libz
ifeq ($(HOST_OS),windows)
LOCAL_LDLIBS += -lws2_32
else
LOCAL_MODULE := libext4_utils
LOCAL_MODULE_TAGS := optional
LOCAL_C_INCLUDES += external/zlib
-LOCAL_SHARED_LIBRARIES := libz
+LOCAL_SHARED_LIBRARIES := libsparse libz
ifeq ($(HAVE_SELINUX), true)
LOCAL_C_INCLUDES += external/libselinux/include
LOCAL_MODULE := libext4_utils
LOCAL_MODULE_TAGS := optional
LOCAL_C_INCLUDES += external/zlib
+LOCAL_STATIC_LIBRARIES += libsparse
ifeq ($(HAVE_SELINUX), true)
LOCAL_C_INCLUDES += external/libselinux/include
LOCAL_SRC_FILES := ext2simg.c
LOCAL_MODULE := ext2simg
LOCAL_MODULE_TAGS := optional
-LOCAL_SHARED_LIBRARIES += libext4_utils libz
+LOCAL_SHARED_LIBRARIES += libext4_utils libsparse libz
ifeq ($(HAVE_SELINUX), true)
LOCAL_C_INCLUDES += external/libselinux/include
LOCAL_SRC_FILES := ext2simg.c
LOCAL_MODULE := ext2simg
LOCAL_MODULE_TAGS := optional
-LOCAL_STATIC_LIBRARIES += libext4_utils libz
+LOCAL_STATIC_LIBRARIES += libext4_utils libsparse libz
ifeq ($(HAVE_SELINUX), true)
LOCAL_C_INCLUDES += external/libselinux/include
include $(BUILD_HOST_EXECUTABLE)
include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := simg2img.c \
- sparse_crc32.c
-LOCAL_MODULE := simg2img
-LOCAL_MODULE_TAGS := debug
-
-include $(BUILD_HOST_EXECUTABLE)
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := simg2img.c \
- sparse_crc32.c
-LOCAL_MODULE := simg2img
-LOCAL_MODULE_TAGS := optional
-
-include $(BUILD_EXECUTABLE)
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := img2simg.c
-LOCAL_MODULE := img2simg
-LOCAL_MODULE_TAGS := debug
-
-include $(BUILD_HOST_EXECUTABLE)
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := img2simg.c
-LOCAL_MODULE := img2simg
-LOCAL_MODULE_TAGS := optional
-
-include $(BUILD_EXECUTABLE)
-
-include $(CLEAR_VARS)
LOCAL_SRC_FILES := setup_fs.c
LOCAL_MODULE := setup_fs
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := ext4fixup_main.c
LOCAL_MODULE := ext4fixup
LOCAL_MODULE_TAGS := optional
-LOCAL_SHARED_LIBRARIES += libext4_utils libz
+LOCAL_SHARED_LIBRARIES += libext4_utils libsparse libz
include $(BUILD_EXECUTABLE)
LOCAL_SRC_FILES := ext4fixup_main.c
LOCAL_MODULE := ext4fixup
LOCAL_MODULE_TAGS := optional
-LOCAL_STATIC_LIBRARIES += libext4_utils libz
+LOCAL_STATIC_LIBRARIES += libext4_utils libsparse libz
include $(BUILD_HOST_EXECUTABLE)
include $(BUILD_PREBUILT)
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := simg_dump.py
-LOCAL_MODULE_TAGS := debug
-LOCAL_SRC_FILES := simg_dump.py
-LOCAL_MODULE_CLASS := EXECUTABLES
-LOCAL_IS_HOST_MODULE := true
-
-include $(BUILD_PREBUILT)
-
endif
+
+include $(LOCAL_PATH)/libsparse/Android.mk
#include "ext4_utils.h"
#include "allocate.h"
-#include "backed_block.h"
#include "ext4.h"
+#include <sparse/sparse.h>
+
#include <stdio.h>
#include <stdlib.h>
* limitations under the License.
*/
+#include <sparse/sparse.h>
+
#include "ext4_utils.h"
#include "make_ext4fs.h"
-#include "output_file.h"
-#include "backed_block.h"
#include "allocate.h"
#include <sys/types.h>
outfd = STDOUT_FILENO;
}
- write_ext4_image(outfd, gzip, sparse, crc, 0);
+ write_ext4_image(outfd, gzip, sparse, crc);
close(outfd);
return 0;
*/
#include "ext4_utils.h"
-#include "output_file.h"
-#include "backed_block.h"
#include "uuid.h"
#include "allocate.h"
#include "indirect.h"
#include "extent.h"
+#include <sparse/sparse.h>
+
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
return 0;
}
-struct count_chunks {
- u32 chunks;
- u64 cur_ptr;
-};
-
-void count_data_block(void *priv, u64 off, u8 *data, int len)
-{
- struct count_chunks *count_chunks = priv;
- if (off > count_chunks->cur_ptr)
- count_chunks->chunks++;
- count_chunks->cur_ptr = off + ALIGN(len, info.block_size);
- count_chunks->chunks++;
-}
-
-void count_fill_block(void *priv, u64 off, u32 fill_val, int len)
-{
- struct count_chunks *count_chunks = priv;
- if (off > count_chunks->cur_ptr)
- count_chunks->chunks++;
- count_chunks->cur_ptr = off + ALIGN(len, info.block_size);
- count_chunks->chunks++;
-}
-
-void count_file_block(void *priv, u64 off, const char *file,
- off64_t offset, int len)
-{
- struct count_chunks *count_chunks = priv;
- if (off > count_chunks->cur_ptr)
- count_chunks->chunks++;
- count_chunks->cur_ptr = off + ALIGN(len, info.block_size);
- count_chunks->chunks++;
-}
-
-int count_sparse_chunks()
-{
- struct count_chunks count_chunks = {0, 0};
-
- for_each_data_block(count_data_block, count_file_block, count_fill_block, &count_chunks);
-
- if (count_chunks.cur_ptr != (u64) info.len)
- count_chunks.chunks++;
-
- return count_chunks.chunks;
-}
-
-static void ext4_write_data_block(void *priv, u64 off, u8 *data, int len)
-{
- write_data_block(priv, off, data, len);
-}
-
-static void ext4_write_fill_block(void *priv, u64 off, u32 fill_val, int len)
-{
- write_fill_block(priv, off, fill_val, len);
-}
-
-static void ext4_write_data_file(void *priv, u64 off, const char *file,
- off64_t offset, int len)
-{
- write_data_file(priv, off, file, offset, len);
-}
-
/* Write the filesystem image to a file */
-void write_ext4_image(int fd, int gz, int sparse, int crc, int wipe)
+void write_ext4_image(int fd, int gz, int sparse, int crc)
{
- int ret = 0;
-
- struct output_file *out = open_output_fd(fd, gz, sparse,
- count_sparse_chunks(), crc, wipe);
-
- if (!out)
- return;
-
- for_each_data_block(ext4_write_data_block, ext4_write_data_file, ext4_write_fill_block, out);
-
- pad_output_file(out, info.len);
-
- close_output_file(out);
+ write_sparse_image(fd, gz, sparse, crc, info.block_size, info.len);
}
/* Compute the rest of the parameters of the filesystem from the basic info */
#define _GNU_SOURCE
#endif
#define _FILE_OFFSET_BITS 64
-#define _LARGEFILE64_SOURCE
+#define _LARGEFILE64_SOURCE 1
#include <sys/types.h>
#include <unistd.h>
}
int ext4_bg_has_super_block(int bg);
-void write_ext4_image(int fd, int gz, int sparse, int crc,
- int wipe);
+void write_ext4_image(int fd, int gz, int sparse, int crc);
void ext4_create_fs_aux_info(void);
void ext4_free_fs_aux_info(void);
void ext4_fill_in_sb(void);
#include "ext4_utils.h"
#include "make_ext4fs.h"
#include "ext4_extents.h"
-#include "output_file.h"
-#include "backed_block.h"
#include "allocate.h"
#include "ext4fixup.h"
+#include <sparse/sparse.h>
+
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "ext4_utils.h"
#include "ext4.h"
#include "ext4_extents.h"
-#include "backed_block.h"
#include "extent.h"
+#include <sparse/sparse.h>
+
#include <stdlib.h>
#include <stdio.h>
#include "ext4_utils.h"
#include "ext4.h"
#include "ext4_extents.h"
-#include "backed_block.h"
#include "indirect.h"
#include "allocate.h"
+#include <sparse/sparse.h>
+
#include <stdlib.h>
#include <stdio.h>
--- /dev/null
+# Copyright 2010 The Android Open Source Project
+
+LOCAL_PATH:= $(call my-dir)
+
+libsparse_src_files := \
+ backed_block.c \
+ output_file.c \
+ sparse.c \
+ sparse_crc32.c
+
+include $(CLEAR_VARS)
+
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_SRC_FILES := $(libsparse_src_files)
+LOCAL_MODULE := libsparse
+LOCAL_MODULE_TAGS := optional
+LOCAL_STATIC_LIBRARIES := libz
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/include external/zlib
+
+include $(BUILD_HOST_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_SRC_FILES := $(libsparse_src_files)
+LOCAL_MODULE := libsparse
+LOCAL_MODULE_TAGS := optional
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/include external/zlib
+LOCAL_SHARED_LIBRARIES := libz
+
+include $(BUILD_SHARED_LIBRARY)
+
+include $(CLEAR_VARS)
+
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_SRC_FILES := $(libsparse_src_files)
+LOCAL_MODULE := libsparse
+LOCAL_MODULE_TAGS := optional
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/include external/zlib
+LOCAL_STATIC_LIBRARIES := libz
+
+include $(BUILD_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := simg2img.c \
+ sparse_crc32.c
+LOCAL_MODULE := simg2img
+LOCAL_MODULE_TAGS := debug
+
+include $(BUILD_HOST_EXECUTABLE)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := simg2img.c \
+ sparse_crc32.c
+LOCAL_MODULE := simg2img
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_EXECUTABLE)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := img2simg.c
+LOCAL_MODULE := img2simg
+LOCAL_MODULE_TAGS := debug
+
+include $(BUILD_HOST_EXECUTABLE)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := img2simg.c
+LOCAL_MODULE := img2simg
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_EXECUTABLE)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := simg_dump.py
+LOCAL_MODULE_TAGS := debug
+LOCAL_SRC_FILES := simg_dump.py
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_IS_HOST_MODULE := true
+
+include $(BUILD_PREBUILT)
* limitations under the License.
*/
-#include "ext4_utils.h"
+#include <sparse/sparse.h>
+
#include "backed_block.h"
+#include "sparse_defs.h"
#include <stdlib.h>
+#include <string.h>
struct data_block {
u32 block;
u32 len;
- u8 *data;
+ void *data;
const char *filename;
off64_t offset;
struct data_block *next;
}
/* Queues a fill block of memory to be written to the specified data blocks */
-void queue_fill_block(u32 fill_val, u32 len, u32 block)
+void queue_fill_block(unsigned int fill_val, unsigned int len, unsigned int block)
{
struct data_block *db = malloc(sizeof(struct data_block));
- if (db == NULL)
- critical_error_errno("malloc");
+ if (db == NULL) {
+ error_errno("malloc");
+ return;
+ }
db->block = block;
db->len = len;
}
/* Queues a block of memory to be written to the specified data blocks */
-void queue_data_block(u8 *data, u32 len, u32 block)
+void queue_data_block(void *data, unsigned int len, unsigned int block)
{
struct data_block *db = malloc(sizeof(struct data_block));
- if (db == NULL)
- critical_error_errno("malloc");
+ if (db == NULL) {
+ error_errno("malloc");
+ return;
+ }
db->block = block;
db->len = len;
}
/* Queues a chunk of a file on disk to be written to the specified data blocks */
-void queue_data_file(const char *filename, off64_t offset, u32 len,
- u32 block)
+void queue_data_file(const char *filename, off64_t offset, unsigned int len,
+ unsigned int block)
{
struct data_block *db = malloc(sizeof(struct data_block));
- if (db == NULL)
- critical_error_errno("malloc");
+ if (db == NULL) {
+ error_errno("malloc");
+ return;
+ }
db->block = block;
db->len = len;
data block, and file_func for each contiguous file block */
void for_each_data_block(data_block_callback_t data_func,
data_block_file_callback_t file_func,
- data_block_fill_callback_t fill_func, void *priv)
+ data_block_fill_callback_t fill_func, void *priv, unsigned int block_size)
{
struct data_block *db;
u32 last_block = 0;
for (db = data_blocks; db; db = db->next) {
if (db->block < last_block)
error("data blocks out of order: %u < %u", db->block, last_block);
- last_block = db->block + DIV_ROUND_UP(db->len, info.block_size) - 1;
+ last_block = db->block + DIV_ROUND_UP(db->len, block_size) - 1;
if (db->filename)
- file_func(priv, (u64)db->block * info.block_size, db->filename, db->offset, db->len);
+ file_func(priv, (u64)db->block * block_size, db->filename, db->offset, db->len);
else if (db->fill)
- fill_func(priv, (u64)db->block * info.block_size, db->fill_val, db->len);
+ fill_func(priv, (u64)db->block * block_size, db->fill_val, db->len);
else
- data_func(priv, (u64)db->block * info.block_size, db->data, db->len);
+ data_func(priv, (u64)db->block * block_size, db->data, db->len);
}
}
#ifndef _BACKED_BLOCK_H_
#define _BACKED_BLOCK_H_
-#include "ext4_utils.h"
-#include "output_file.h"
+#include <sparse/sparse.h>
-typedef void (*data_block_callback_t)(void *priv, u64 off, u8 *data, int len);
-typedef void (*data_block_fill_callback_t)(void *priv, u64 off, u32 fill_val, int len);
-typedef void (*data_block_file_callback_t)(void *priv, u64 off,
+typedef void (*data_block_callback_t)(void *priv, off64_t off, void *data, int len);
+typedef void (*data_block_fill_callback_t)(void *priv, off64_t off, unsigned int fill_val, int len);
+typedef void (*data_block_file_callback_t)(void *priv, off64_t off,
const char *file, off64_t offset,
int len);
-void queue_data_block(u8 *data, u32 len, u32 block);
-void queue_fill_block(u32 fill_val, u32 len, u32 block);
-void queue_data_file(const char *filename, off64_t offset, u32 len,
- u32 block);
void for_each_data_block(data_block_callback_t data_func,
data_block_file_callback_t file_func,
- data_block_fill_callback_t fill_func, void *priv);
-void free_data_blocks();
+ data_block_fill_callback_t fill_func, void *priv, unsigned int);
#endif
#define DEFAULT_CHUNK_SIZE "64M"
#define DEFAULT_SUFFIX "%03d"
-#include "ext4_utils.h"
#include "sparse_format.h"
#if 0 /* endian.h is not on all platforms */
# include <endian.h>
--- /dev/null
+/*
+ * Copyright (C) 2012 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 _LIBSPARSE_SPARSE_H_
+#define _LIBSPARSE_SPARSE_H_
+
+#define _FILE_OFFSET_BITS 64
+#define _LARGEFILE64_SOURCE 1
+#include <sys/types.h>
+#include <unistd.h>
+
+#if defined(__APPLE__) && defined(__MACH__)
+#define off64_t off_t
+#endif
+
+void write_sparse_image(int fd, int gz, int sparse, int crc, unsigned int block_size, off64_t len);
+void queue_data_block(void *data, unsigned int len, unsigned int block);
+void queue_fill_block(unsigned int fill_val, unsigned int len, unsigned int block);
+void queue_data_file(const char *filename, off64_t offset, unsigned int len,
+ unsigned int block);
+void free_data_blocks();
+
+#endif
* limitations under the License.
*/
-#include "ext4_utils.h"
+#include <sparse/sparse.h>
+
#include "output_file.h"
#include "sparse_format.h"
#include "sparse_crc32.h"
-#include "wipe.h"
#include <fcntl.h>
#include <stdbool.h>
-#include <sys/types.h>
+#include <stdlib.h>
+#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define O_BINARY 0
#endif
-
#if defined(__APPLE__) && defined(__MACH__)
#define lseek64 lseek
-#define off64_t off_t
+#define ftruncate64 ftruncate
+#define mmap64 mmap
+#endif
+
+#ifdef __BIONIC__
+extern void* __mmap2(void *, size_t, int, int, int, off_t);
+static inline void *mmap64(void *addr, size_t length, int prot, int flags,
+ int fd, off64_t offset)
+{
+ return __mmap2(addr, length, prot, flags, fd, offset >> 12);
+}
#endif
#define SPARSE_HEADER_MAJOR_VER 1
gzFile gz_fd;
bool close_fd;
int sparse;
- u64 cur_out_ptr;
+ off64_t cur_out_ptr;
u32 chunk_cnt;
u32 crc32;
struct output_file_ops *ops;
int use_crc;
+ unsigned int block_size;
+ off64_t len;
};
static int file_seek(struct output_file *out, off64_t off)
//DBG printf("skip chunk: 0x%llx bytes\n", skip_len);
- if (skip_len % info.block_size) {
+ if (skip_len % out->block_size) {
error("don't care size %llu is not a multiple of the block size %u",
- skip_len, info.block_size);
+ skip_len, out->block_size);
return -1;
}
/* We are skipping data, so emit a don't care chunk. */
chunk_header.chunk_type = CHUNK_TYPE_DONT_CARE;
chunk_header.reserved1 = 0;
- chunk_header.chunk_sz = skip_len / info.block_size;
+ chunk_header.chunk_sz = skip_len / out->block_size;
chunk_header.total_sz = CHUNK_HEADER_LEN;
ret = out->ops->write(out, (u8 *)&chunk_header, sizeof(chunk_header));
if (ret < 0)
return 0;
}
-static int write_chunk_fill(struct output_file *out, u64 off, u32 fill_val, int len)
+static int write_chunk_fill(struct output_file *out, off64_t off, u32 fill_val, int len)
{
chunk_header_t chunk_header;
int rnd_up_len, zero_len, count;
emit_skip_chunk(out, off - out->cur_out_ptr);
}
- if (off % info.block_size) {
+ if (off % out->block_size) {
error("write chunk offset %llu is not a multiple of the block size %u",
- off, info.block_size);
+ off, out->block_size);
return -1;
}
}
/* Round up the file length to a multiple of the block size */
- rnd_up_len = (len + (info.block_size - 1)) & (~(info.block_size -1));
+ rnd_up_len = (len + (out->block_size - 1)) & (~(out->block_size -1));
/* Finally we can safely emit a chunk of data */
chunk_header.chunk_type = CHUNK_TYPE_FILL;
chunk_header.reserved1 = 0;
- chunk_header.chunk_sz = rnd_up_len / info.block_size;
+ chunk_header.chunk_sz = rnd_up_len / out->block_size;
chunk_header.total_sz = CHUNK_HEADER_LEN + sizeof(fill_val);
ret = out->ops->write(out, (u8 *)&chunk_header, sizeof(chunk_header));
if (out->use_crc) {
/* Initialize fill_buf with the fill_val */
- for (i = 0; i < (info.block_size / sizeof(u32)); i++) {
+ for (i = 0; i < (out->block_size / sizeof(u32)); i++) {
fill_buf[i] = fill_val;
}
count = chunk_header.chunk_sz;
while (count) {
- out->crc32 = sparse_crc32(out->crc32, fill_buf, info.block_size);
+ out->crc32 = sparse_crc32(out->crc32, fill_buf, out->block_size);
count--;
}
}
return 0;
}
-static int write_chunk_raw(struct output_file *out, u64 off, u8 *data, int len)
+static int write_chunk_raw(struct output_file *out, off64_t off, u8 *data, int len)
{
chunk_header_t chunk_header;
int rnd_up_len, zero_len;
emit_skip_chunk(out, off - out->cur_out_ptr);
}
- if (off % info.block_size) {
+ if (off % out->block_size) {
error("write chunk offset %llu is not a multiple of the block size %u",
- off, info.block_size);
+ off, out->block_size);
return -1;
}
}
/* Round up the file length to a multiple of the block size */
- rnd_up_len = (len + (info.block_size - 1)) & (~(info.block_size -1));
+ rnd_up_len = (len + (out->block_size - 1)) & (~(out->block_size -1));
zero_len = rnd_up_len - len;
/* Finally we can safely emit a chunk of data */
chunk_header.chunk_type = CHUNK_TYPE_RAW;
chunk_header.reserved1 = 0;
- chunk_header.chunk_sz = rnd_up_len / info.block_size;
+ chunk_header.chunk_sz = rnd_up_len / out->block_size;
chunk_header.total_sz = CHUNK_HEADER_LEN + rnd_up_len;
ret = out->ops->write(out, (u8 *)&chunk_header, sizeof(chunk_header));
out->ops->close(out);
}
-struct output_file *open_output_fd(int fd, int gz, int sparse,
- int chunks, int crc, int wipe)
+struct output_file *open_output_fd(int fd, unsigned int block_size, off64_t len,
+ int gz, int sparse, int chunks, int crc)
{
int ret;
struct output_file *out = malloc(sizeof(struct output_file));
error_errno("malloc struct out");
return NULL;
}
- zero_buf = malloc(info.block_size);
+ zero_buf = malloc(out->block_size);
if (!zero_buf) {
error_errno("malloc zero_buf");
free(out);
return NULL;
}
- memset(zero_buf, '\0', info.block_size);
+ memset(zero_buf, '\0', out->block_size);
if (gz) {
out->ops = &gz_file_ops;
out->crc32 = 0;
out->use_crc = crc;
- if (wipe)
- wipe_block_device(out->fd, info.len);
+ out->len = len;
+ out->block_size = block_size;
if (out->sparse) {
- sparse_header.blk_sz = info.block_size,
- sparse_header.total_blks = info.len / info.block_size,
+ sparse_header.blk_sz = out->block_size,
+ sparse_header.total_blks = out->len / out->block_size,
sparse_header.total_chunks = chunks;
if (out->use_crc)
sparse_header.total_chunks++;
return out;
}
-struct output_file *open_output_file(const char *filename, int gz, int sparse,
- int chunks, int crc, int wipe) {
-
+struct output_file *open_output_file(const char *filename,
+ unsigned int block_size, off64_t len,
+ int gz, int sparse, int chunks, int crc)
+{
int fd;
struct output_file *file;
fd = STDOUT_FILENO;
}
- file = open_output_fd(fd, gz, sparse, chunks, crc, wipe);
+ file = open_output_fd(fd, block_size, len, gz, sparse, chunks, crc);
if (!file) {
close(fd);
return NULL;
return file;
}
-void pad_output_file(struct output_file *out, u64 len)
+void pad_output_file(struct output_file *out, off64_t len)
{
int ret;
- if (len > (u64) info.len) {
+ if (len > out->len) {
error("attempted to pad file %llu bytes past end of filesystem",
- len - info.len);
+ len - out->len);
return;
}
if (out->sparse) {
}
/* Write a contiguous region of data blocks from a memory buffer */
-void write_data_block(struct output_file *out, u64 off, u8 *data, int len)
+void write_data_block(struct output_file *out, off64_t off, void *data, int len)
{
int ret;
- if (off + len > (u64) info.len) {
+ if (off + len > out->len) {
error("attempted to write block %llu past end of filesystem",
- off + len - info.len);
+ off + len - out->len);
return;
}
}
/* Write a contiguous region of data blocks with a fill value */
-void write_fill_block(struct output_file *out, u64 off, u32 fill_val, int len)
+void write_fill_block(struct output_file *out, off64_t off, unsigned int fill_val, int len)
{
int ret;
unsigned int i;
int write_len;
u32 fill_buf[4096/sizeof(u32)]; /* Maximum size of a block */
- if (off + len > (u64) info.len) {
+ if (off + len > out->len) {
error("attempted to write block %llu past end of filesystem",
- off + len - info.len);
+ off + len - out->len);
return;
}
}
/* Write a contiguous region of data blocks from a file */
-void write_data_file(struct output_file *out, u64 off, const char *file,
+void write_data_file(struct output_file *out, off64_t off, const char *file,
off64_t offset, int len)
{
int ret;
int aligned_diff;
int buffer_size;
- if (off + len >= (u64) info.len) {
+ if (off + len >= out->len) {
error("attempted to write block %llu past end of filesystem",
- off + len - info.len);
+ off + len - out->len);
return;
}
#ifndef _OUTPUT_FILE_H_
#define _OUTPUT_FILE_H_
+#include <sparse/sparse.h>
+
struct output_file;
-struct output_file *open_output_file(const char *filename, int gz, int sparse,
- int chunks, int crc, int wipe);
-struct output_file *open_output_fd(int fd, int gz, int sparse,
- int chunks, int crc, int wipe);
-void write_data_block(struct output_file *out, u64 off, u8 *data, int len);
-void write_fill_block(struct output_file *out, u64 off, u32 fill_val, int len);
-void write_data_file(struct output_file *out, u64 off, const char *file,
+struct output_file *open_output_file(const char *filename,
+ unsigned int block_size, off64_t len,
+ int gz, int sparse, int chunks, int crc);
+struct output_file *open_output_fd(int fd, unsigned int block_size, off64_t len,
+ int gz, int sparse, int chunks, int crc);
+void write_data_block(struct output_file *out, off64_t off, void *data, int len);
+void write_fill_block(struct output_file *out, off64_t off, unsigned int fill_val, int len);
+void write_data_file(struct output_file *out, off64_t off, const char *file,
off64_t offset, int len);
-void pad_output_file(struct output_file *out, u64 len);
+void pad_output_file(struct output_file *out, off64_t len);
void close_output_file(struct output_file *out);
#endif
* limitations under the License.
*/
-#include "ext4_utils.h"
+#define _FILE_OFFSET_BITS 64
+#define _LARGEFILE64_SOURCE 1
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "sparse_defs.h"
#include "sparse_format.h"
#include "sparse_crc32.h"
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
-#include <fcntl.h>
-#include <stdio.h>
#define COPY_BUF_SIZE (1024*1024)
u8 *copybuf;
--- /dev/null
+/*
+ * Copyright (C) 2012 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 "output_file.h"
+#include "backed_block.h"
+#include "sparse_defs.h"
+
+struct count_chunks {
+ unsigned int chunks;
+ off64_t cur_ptr;
+ unsigned int block_size;
+};
+
+static void count_data_block(void *priv, off64_t off, void *data, int len)
+{
+ struct count_chunks *count_chunks = priv;
+ if (off > count_chunks->cur_ptr)
+ count_chunks->chunks++;
+ count_chunks->cur_ptr = off + ALIGN(len, count_chunks->block_size);
+ count_chunks->chunks++;
+}
+
+static void count_fill_block(void *priv, off64_t off, unsigned int fill_val, int len)
+{
+ struct count_chunks *count_chunks = priv;
+ if (off > count_chunks->cur_ptr)
+ count_chunks->chunks++;
+ count_chunks->cur_ptr = off + ALIGN(len, count_chunks->block_size);
+ count_chunks->chunks++;
+}
+
+static void count_file_block(void *priv, off64_t off, const char *file,
+ off64_t offset, int len)
+{
+ struct count_chunks *count_chunks = priv;
+ if (off > count_chunks->cur_ptr)
+ count_chunks->chunks++;
+ count_chunks->cur_ptr = off + ALIGN(len, count_chunks->block_size);
+ count_chunks->chunks++;
+}
+
+static int count_sparse_chunks(unsigned int block_size, off64_t len)
+{
+ struct count_chunks count_chunks = {0, 0, block_size};
+
+ for_each_data_block(count_data_block, count_file_block, count_fill_block, &count_chunks, block_size);
+
+ if (count_chunks.cur_ptr != len)
+ count_chunks.chunks++;
+
+ return count_chunks.chunks;
+}
+
+static void ext4_write_data_block(void *priv, off64_t off, void *data, int len)
+{
+ write_data_block(priv, off, data, len);
+}
+
+static void ext4_write_fill_block(void *priv, off64_t off, unsigned int fill_val, int len)
+{
+ write_fill_block(priv, off, fill_val, len);
+}
+
+static void ext4_write_data_file(void *priv, off64_t off, const char *file,
+ off64_t offset, int len)
+{
+ write_data_file(priv, off, file, offset, len);
+}
+
+/* Write the filesystem image to a file */
+void write_sparse_image(int fd, int gz, int sparse, int crc, unsigned int block_size, off64_t len)
+{
+ int chunks = count_sparse_chunks(block_size, len);
+ struct output_file *out = open_output_fd(fd, block_size, len,
+ gz, sparse, chunks, crc);
+
+ if (!out)
+ return;
+
+ for_each_data_block(ext4_write_data_block, ext4_write_data_file, ext4_write_fill_block, out, block_size);
+
+ pad_output_file(out, len);
+
+ close_output_file(out);
+}
*/
/* Code taken from FreeBSD 8 */
-#include "ext4_utils.h"
+#include <stdint.h>
-static u32 crc32_tab[] = {
+static uint32_t crc32_tab[] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
* in sys/libkern.h, where it can be inlined.
*/
-u32 sparse_crc32(u32 crc_in, const void *buf, int size)
+uint32_t sparse_crc32(uint32_t crc_in, const void *buf, int size)
{
- const u8 *p = buf;
- u32 crc;
+ const uint8_t *p = buf;
+ uint32_t crc;
crc = crc_in ^ ~0U;
while (size--)
--- /dev/null
+/*
+ * 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 _LIBSPARSE_SPARSE_DEFS_
+#define _LIBSPARSE_SPARSE_DEFS_
+
+#include <errno.h>
+#include <stdio.h>
+
+#define __le64 u64
+#define __le32 u32
+#define __le16 u16
+
+#define __be64 u64
+#define __be32 u32
+#define __be16 u16
+
+#define __u64 u64
+#define __u32 u32
+#define __u16 u16
+#define __u8 u8
+
+typedef unsigned long long u64;
+typedef signed long long s64;
+typedef unsigned int u32;
+typedef unsigned short int u16;
+typedef unsigned char u8;
+
+#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
+#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
+
+#define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); } while (0)
+#define error_errno(s, args...) error(s ": %s", ##args, strerror(errno))
+
+#endif
* limitations under the License.
*/
+#ifndef _LIBSPARSE_SPARSE_FORMAT_H_
+#define _LIBSPARSE_SPARSE_FORMAT_H_
+#include "sparse_defs.h"
+
typedef struct sparse_header {
__le32 magic; /* 0xed26ff3a */
__le16 major_version; /* (0x1) - reject images with higher major versions */
* For a CRC32 chunk, it's 4 bytes of CRC32
*/
+#endif
*/
#include "make_ext4fs.h"
-#include "output_file.h"
#include "ext4_utils.h"
#include "allocate.h"
#include "contents.h"
#include "uuid.h"
-#include "backed_block.h"
+#include "wipe.h"
+
+#include <sparse/sparse.h>
#include <assert.h>
#include <dirent.h>
aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
aux_info.sb->s_blocks_count_lo);
- write_ext4_image(fd, gzip, sparse, crc, wipe);
+ if (wipe)
+ wipe_block_device(fd, info.len);
+
+ write_ext4_image(fd, gzip, sparse, crc);
return 0;
}