nearly working nandroid, built against libc
This commit is contained in:
parent
3ab130fa9e
commit
8ce0be4956
@ -49,7 +49,7 @@ LOCAL_STATIC_LIBRARIES += libamend
|
||||
include $(BUILD_EXECUTABLE)
|
||||
|
||||
include $(commands_recovery_local_path)/amend/Android.mk
|
||||
include $(commands_recovery_local_path)/dump_image/Android.mk
|
||||
include $(commands_recovery_local_path)/nandroid/Android.mk
|
||||
include $(commands_recovery_local_path)/minui/Android.mk
|
||||
include $(commands_recovery_local_path)/minzip/Android.mk
|
||||
include $(commands_recovery_local_path)/mtdutils/Android.mk
|
||||
|
@ -1,15 +0,0 @@
|
||||
ifneq ($(TARGET_SIMULATOR),true)
|
||||
ifeq ($(TARGET_ARCH),arm)
|
||||
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_SRC_FILES := dump_image.c mtdutils.c ../mtdutils/mounts.c
|
||||
LOCAL_MODULE := recovery_dump_image
|
||||
LOCAL_MODULE_TAGS := eng
|
||||
LOCAL_STATIC_LIBRARIES := libcutils libc
|
||||
LOCAL_MODULE_STEM := dump_image
|
||||
LOCAL_FORCE_STATIC_EXECUTABLE := true
|
||||
include $(BUILD_EXECUTABLE)
|
||||
|
||||
endif # TARGET_ARCH == arm
|
||||
endif # !TARGET_SIMULATOR
|
@ -1,600 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2007 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 <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/mount.h> // for _IOW, _IOR, mount()
|
||||
#include <sys/stat.h>
|
||||
#include <mtd/mtd-user.h>
|
||||
#undef NDEBUG
|
||||
#include <assert.h>
|
||||
|
||||
#include "mtdutils.h"
|
||||
|
||||
struct MtdPartition {
|
||||
int device_index;
|
||||
unsigned int size;
|
||||
unsigned int erase_size;
|
||||
char *name;
|
||||
};
|
||||
|
||||
struct MtdReadContext {
|
||||
const MtdPartition *partition;
|
||||
char *buffer;
|
||||
size_t read_size;
|
||||
size_t consumed;
|
||||
int fd;
|
||||
};
|
||||
|
||||
struct MtdWriteContext {
|
||||
const MtdPartition *partition;
|
||||
char *buffer;
|
||||
size_t stored;
|
||||
int fd;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
MtdPartition *partitions;
|
||||
int partitions_allocd;
|
||||
int partition_count;
|
||||
} MtdState;
|
||||
|
||||
static MtdState g_mtd_state = {
|
||||
NULL, // partitions
|
||||
0, // partitions_allocd
|
||||
-1 // partition_count
|
||||
};
|
||||
|
||||
#define MTD_PROC_FILENAME "/proc/mtd"
|
||||
|
||||
int
|
||||
mtd_scan_partitions()
|
||||
{
|
||||
char buf[2048];
|
||||
const char *bufp;
|
||||
int fd;
|
||||
int i;
|
||||
ssize_t nbytes;
|
||||
|
||||
if (g_mtd_state.partitions == NULL) {
|
||||
const int nump = 32;
|
||||
MtdPartition *partitions = malloc(nump * sizeof(*partitions));
|
||||
if (partitions == NULL) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
g_mtd_state.partitions = partitions;
|
||||
g_mtd_state.partitions_allocd = nump;
|
||||
memset(partitions, 0, nump * sizeof(*partitions));
|
||||
}
|
||||
g_mtd_state.partition_count = 0;
|
||||
|
||||
/* Initialize all of the entries to make things easier later.
|
||||
* (Lets us handle sparsely-numbered partitions, which
|
||||
* may not even be possible.)
|
||||
*/
|
||||
for (i = 0; i < g_mtd_state.partitions_allocd; i++) {
|
||||
MtdPartition *p = &g_mtd_state.partitions[i];
|
||||
if (p->name != NULL) {
|
||||
free(p->name);
|
||||
p->name = NULL;
|
||||
}
|
||||
p->device_index = -1;
|
||||
}
|
||||
|
||||
/* Open and read the file contents.
|
||||
*/
|
||||
fd = open(MTD_PROC_FILENAME, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
goto bail;
|
||||
}
|
||||
nbytes = read(fd, buf, sizeof(buf) - 1);
|
||||
close(fd);
|
||||
if (nbytes < 0) {
|
||||
goto bail;
|
||||
}
|
||||
buf[nbytes] = '\0';
|
||||
|
||||
/* Parse the contents of the file, which looks like:
|
||||
*
|
||||
* # cat /proc/mtd
|
||||
* dev: size erasesize name
|
||||
* mtd0: 00080000 00020000 "bootloader"
|
||||
* mtd1: 00400000 00020000 "mfg_and_gsm"
|
||||
* mtd2: 00400000 00020000 "0000000c"
|
||||
* mtd3: 00200000 00020000 "0000000d"
|
||||
* mtd4: 04000000 00020000 "system"
|
||||
* mtd5: 03280000 00020000 "userdata"
|
||||
*/
|
||||
bufp = buf;
|
||||
while (nbytes > 0) {
|
||||
int mtdnum, mtdsize, mtderasesize;
|
||||
int matches;
|
||||
char mtdname[64];
|
||||
mtdname[0] = '\0';
|
||||
mtdnum = -1;
|
||||
|
||||
matches = sscanf(bufp, "mtd%d: %x %x \"%63[^\"]",
|
||||
&mtdnum, &mtdsize, &mtderasesize, mtdname);
|
||||
/* This will fail on the first line, which just contains
|
||||
* column headers.
|
||||
*/
|
||||
if (matches == 4) {
|
||||
MtdPartition *p = &g_mtd_state.partitions[mtdnum];
|
||||
p->device_index = mtdnum;
|
||||
p->size = mtdsize;
|
||||
p->erase_size = mtderasesize;
|
||||
p->name = strdup(mtdname);
|
||||
if (p->name == NULL) {
|
||||
errno = ENOMEM;
|
||||
goto bail;
|
||||
}
|
||||
g_mtd_state.partition_count++;
|
||||
}
|
||||
|
||||
/* Eat the line.
|
||||
*/
|
||||
while (nbytes > 0 && *bufp != '\n') {
|
||||
bufp++;
|
||||
nbytes--;
|
||||
}
|
||||
if (nbytes > 0) {
|
||||
bufp++;
|
||||
nbytes--;
|
||||
}
|
||||
}
|
||||
|
||||
return g_mtd_state.partition_count;
|
||||
|
||||
bail:
|
||||
// keep "partitions" around so we can free the names on a rescan.
|
||||
g_mtd_state.partition_count = -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
const MtdPartition *
|
||||
mtd_find_partition_by_name(const char *name)
|
||||
{
|
||||
if (g_mtd_state.partitions != NULL) {
|
||||
int i;
|
||||
for (i = 0; i < g_mtd_state.partitions_allocd; i++) {
|
||||
MtdPartition *p = &g_mtd_state.partitions[i];
|
||||
if (p->device_index >= 0 && p->name != NULL) {
|
||||
if (strcmp(p->name, name) == 0) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
mtd_mount_partition(const MtdPartition *partition, const char *mount_point,
|
||||
const char *filesystem, int read_only)
|
||||
{
|
||||
const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
|
||||
char devname[64];
|
||||
int rv = -1;
|
||||
|
||||
sprintf(devname, "/dev/block/mtdblock%d", partition->device_index);
|
||||
if (!read_only) {
|
||||
rv = mount(devname, mount_point, filesystem, flags, NULL);
|
||||
}
|
||||
if (read_only || rv < 0) {
|
||||
rv = mount(devname, mount_point, filesystem, flags | MS_RDONLY, 0);
|
||||
if (rv < 0) {
|
||||
printf("Failed to mount %s on %s: %s\n",
|
||||
devname, mount_point, strerror(errno));
|
||||
} else {
|
||||
printf("Mount %s on %s read-only\n", devname, mount_point);
|
||||
}
|
||||
}
|
||||
#if 1 //TODO: figure out why this is happening; remove include of stat.h
|
||||
if (rv >= 0) {
|
||||
/* For some reason, the x bits sometimes aren't set on the root
|
||||
* of mounted volumes.
|
||||
*/
|
||||
struct stat st;
|
||||
rv = stat(mount_point, &st);
|
||||
if (rv < 0) {
|
||||
return rv;
|
||||
}
|
||||
mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
|
||||
if (new_mode != st.st_mode) {
|
||||
printf("Fixing execute permissions for %s\n", mount_point);
|
||||
rv = chmod(mount_point, new_mode);
|
||||
if (rv < 0) {
|
||||
printf("Couldn't fix permissions for %s: %s\n",
|
||||
mount_point, strerror(errno));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return rv;
|
||||
}
|
||||
|
||||
int
|
||||
mtd_partition_info(const MtdPartition *partition,
|
||||
size_t *total_size, size_t *erase_size, size_t *write_size)
|
||||
{
|
||||
char mtddevname[32];
|
||||
sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index);
|
||||
int fd = open(mtddevname, O_RDONLY);
|
||||
if (fd < 0) return -1;
|
||||
|
||||
struct mtd_info_user mtd_info;
|
||||
int ret = ioctl(fd, MEMGETINFO, &mtd_info);
|
||||
close(fd);
|
||||
if (ret < 0) return -1;
|
||||
|
||||
if (total_size != NULL) *total_size = mtd_info.size;
|
||||
if (erase_size != NULL) *erase_size = mtd_info.erasesize;
|
||||
if (write_size != NULL) *write_size = mtd_info.writesize;
|
||||
return 0;
|
||||
}
|
||||
|
||||
MtdReadContext *mtd_read_partition(const MtdPartition *partition)
|
||||
{
|
||||
MtdReadContext *ctx = (MtdReadContext*) malloc(sizeof(MtdReadContext));
|
||||
if (ctx == NULL) return NULL;
|
||||
|
||||
ctx->buffer = malloc(partition->erase_size);
|
||||
if (ctx->buffer == NULL) {
|
||||
free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char mtddevname[32];
|
||||
sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index);
|
||||
ctx->fd = open(mtddevname, O_RDONLY);
|
||||
if (ctx->fd < 0) {
|
||||
free(ctx);
|
||||
free(ctx->buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ctx->partition = partition;
|
||||
ctx->read_size = partition->erase_size;
|
||||
ctx->consumed = ctx->read_size;
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static int read_block(const MtdReadContext *ctx, char *data)
|
||||
{
|
||||
struct mtd_ecc_stats before, after;
|
||||
off_t pos;
|
||||
ssize_t size;
|
||||
|
||||
if (ioctl(ctx->fd, ECCGETSTATS, &before)) {
|
||||
fprintf(stderr, "mtd: ECCGETSTATS error (%s)\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
pos = lseek(ctx->fd, 0, SEEK_CUR);
|
||||
size = ctx->read_size;
|
||||
|
||||
while (pos + size <= (int) ctx->partition->size) {
|
||||
if (lseek(ctx->fd, pos, SEEK_SET) != pos || read(ctx->fd, data, size) != size) {
|
||||
fprintf(stderr, "mtd: read error at 0x%08lx (%s)\n",
|
||||
pos, strerror(errno));
|
||||
} else if (ioctl(ctx->fd, ECCGETSTATS, &after)) {
|
||||
fprintf(stderr, "mtd: ECCGETSTATS error (%s)\n", strerror(errno));
|
||||
return -1;
|
||||
} else if (after.failed != before.failed) {
|
||||
fprintf(stderr, "mtd: ECC errors (%d soft, %d hard) at 0x%08lx\n",
|
||||
after.corrected - before.corrected,
|
||||
after.failed - before.failed, pos);
|
||||
/*
|
||||
* Reset error counts, so next read may succeed.
|
||||
*/
|
||||
before = after;
|
||||
} else {
|
||||
return 0; // Success!
|
||||
}
|
||||
|
||||
pos += ctx->read_size;
|
||||
}
|
||||
|
||||
errno = ENOSPC;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t mtd_read_data(MtdReadContext *ctx, char *data, size_t len)
|
||||
{
|
||||
ssize_t read = 0;
|
||||
while (read < (int) len) {
|
||||
if (ctx->consumed < ctx->read_size) {
|
||||
size_t avail = ctx->read_size - ctx->consumed;
|
||||
size_t copy = len - read < avail ? len - read : avail;
|
||||
memcpy(data + read, ctx->buffer + ctx->consumed, copy);
|
||||
ctx->consumed += copy;
|
||||
read += copy;
|
||||
}
|
||||
|
||||
// Read complete blocks directly into the user's buffer
|
||||
while (ctx->consumed == ctx->read_size &&
|
||||
len - read >= ctx->read_size) {
|
||||
if (read_block(ctx, data + read)) return -1;
|
||||
read += ctx->read_size;
|
||||
}
|
||||
|
||||
// Read the next block into the buffer
|
||||
if (ctx->consumed == ctx->read_size && read < (int) len) {
|
||||
if (read_block(ctx, ctx->buffer)) return -1;
|
||||
ctx->consumed = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return read;
|
||||
}
|
||||
|
||||
ssize_t mtd_read_raw(MtdReadContext *ctx, char *data, size_t len)
|
||||
{
|
||||
static const int SPARE_SIZE = (2048 >> 5);
|
||||
struct mtd_info_user mtd_info;
|
||||
struct mtd_oob_buf oob_buf;
|
||||
struct nand_ecclayout ecc_layout;
|
||||
struct nand_oobfree *fp;
|
||||
unsigned char ecc[SPARE_SIZE];
|
||||
char *src, *dst;
|
||||
int i, n, ret;
|
||||
|
||||
/*
|
||||
* FIXME: These two ioctls should be cached in MtdReadContext.
|
||||
*/
|
||||
ret = ioctl(ctx->fd, MEMGETINFO, &mtd_info);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
ret = ioctl(ctx->fd, ECCGETLAYOUT, &ecc_layout);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
ctx->read_size = mtd_info.writesize;
|
||||
ctx->consumed = ctx->read_size;
|
||||
|
||||
/*
|
||||
* Read next good data block.
|
||||
*/
|
||||
ret = read_block(ctx, data);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
dst = src = data + ctx->read_size;
|
||||
|
||||
/*
|
||||
* Read OOB data for last block read in read_block().
|
||||
*/
|
||||
oob_buf.start = lseek(ctx->fd, 0, SEEK_CUR) - ctx->read_size;
|
||||
oob_buf.length = mtd_info.oobsize;
|
||||
oob_buf.ptr = (unsigned char *) src;
|
||||
|
||||
ret = ioctl(ctx->fd, MEMREADOOB, &oob_buf);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
/*
|
||||
* As yaffs and yaffs2 use mode MEM_OOB_AUTO, but mtdchar uses
|
||||
* MEM_OOB_PLACE, copy the spare data down the hard way.
|
||||
*
|
||||
* Safe away ECC data:
|
||||
*/
|
||||
for (i = 0; i < ecc_layout.eccbytes; i++) {
|
||||
ecc[i] = src[ecc_layout.eccpos[i]];
|
||||
}
|
||||
for ( ; i < SPARE_SIZE; i++) {
|
||||
ecc[i] = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy yaffs2 spare data down.
|
||||
*/
|
||||
n = ecc_layout.oobavail;
|
||||
fp = &ecc_layout.oobfree[0];
|
||||
while (n) {
|
||||
if (fp->offset) {
|
||||
memmove(dst, src + fp->offset, fp->length);
|
||||
}
|
||||
dst += fp->length;
|
||||
n -= fp->length;
|
||||
++fp;
|
||||
}
|
||||
|
||||
/*
|
||||
* Restore ECC data behind spare data.
|
||||
*/
|
||||
memcpy(dst, ecc, (ctx->read_size >> 5) - ecc_layout.oobavail);
|
||||
|
||||
return ctx->read_size + (ctx->read_size >> 5);
|
||||
}
|
||||
|
||||
void mtd_read_close(MtdReadContext *ctx)
|
||||
{
|
||||
close(ctx->fd);
|
||||
free(ctx->buffer);
|
||||
free(ctx);
|
||||
}
|
||||
|
||||
MtdWriteContext *mtd_write_partition(const MtdPartition *partition)
|
||||
{
|
||||
MtdWriteContext *ctx = (MtdWriteContext*) malloc(sizeof(MtdWriteContext));
|
||||
if (ctx == NULL) return NULL;
|
||||
|
||||
ctx->buffer = malloc(partition->erase_size);
|
||||
if (ctx->buffer == NULL) {
|
||||
free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char mtddevname[32];
|
||||
sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index);
|
||||
ctx->fd = open(mtddevname, O_RDWR);
|
||||
if (ctx->fd < 0) {
|
||||
free(ctx->buffer);
|
||||
free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ctx->partition = partition;
|
||||
ctx->stored = 0;
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static int write_block(const MtdPartition *partition, int fd, const char *data)
|
||||
{
|
||||
off_t pos = lseek(fd, 0, SEEK_CUR);
|
||||
if (pos == (off_t) -1) return 1;
|
||||
|
||||
ssize_t size = partition->erase_size;
|
||||
while (pos + size <= (int) partition->size) {
|
||||
loff_t bpos = pos;
|
||||
if (ioctl(fd, MEMGETBADBLOCK, &bpos) > 0) {
|
||||
fprintf(stderr, "mtd: not writing bad block at 0x%08lx\n", pos);
|
||||
pos += partition->erase_size;
|
||||
continue; // Don't try to erase known factory-bad blocks.
|
||||
}
|
||||
|
||||
struct erase_info_user erase_info;
|
||||
erase_info.start = pos;
|
||||
erase_info.length = size;
|
||||
int retry;
|
||||
for (retry = 0; retry < 2; ++retry) {
|
||||
if (ioctl(fd, MEMERASE, &erase_info) < 0) {
|
||||
fprintf(stderr, "mtd: erase failure at 0x%08lx (%s)\n",
|
||||
pos, strerror(errno));
|
||||
continue;
|
||||
}
|
||||
if (lseek(fd, pos, SEEK_SET) != pos ||
|
||||
write(fd, data, size) != size) {
|
||||
fprintf(stderr, "mtd: write error at 0x%08lx (%s)\n",
|
||||
pos, strerror(errno));
|
||||
}
|
||||
|
||||
char verify[size];
|
||||
if (lseek(fd, pos, SEEK_SET) != pos ||
|
||||
read(fd, verify, size) != size) {
|
||||
fprintf(stderr, "mtd: re-read error at 0x%08lx (%s)\n",
|
||||
pos, strerror(errno));
|
||||
continue;
|
||||
}
|
||||
if (memcmp(data, verify, size) != 0) {
|
||||
fprintf(stderr, "mtd: verification error at 0x%08lx (%s)\n",
|
||||
pos, strerror(errno));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (retry > 0) {
|
||||
fprintf(stderr, "mtd: wrote block after %d retries\n", retry);
|
||||
}
|
||||
return 0; // Success!
|
||||
}
|
||||
|
||||
// Try to erase it once more as we give up on this block
|
||||
fprintf(stderr, "mtd: skipping write block at 0x%08lx\n", pos);
|
||||
ioctl(fd, MEMERASE, &erase_info);
|
||||
pos += partition->erase_size;
|
||||
}
|
||||
|
||||
// Ran out of space on the device
|
||||
errno = ENOSPC;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t mtd_write_data(MtdWriteContext *ctx, const char *data, size_t len)
|
||||
{
|
||||
size_t wrote = 0;
|
||||
while (wrote < len) {
|
||||
// Coalesce partial writes into complete blocks
|
||||
if (ctx->stored > 0 || len - wrote < ctx->partition->erase_size) {
|
||||
size_t avail = ctx->partition->erase_size - ctx->stored;
|
||||
size_t copy = len - wrote < avail ? len - wrote : avail;
|
||||
memcpy(ctx->buffer + ctx->stored, data + wrote, copy);
|
||||
ctx->stored += copy;
|
||||
wrote += copy;
|
||||
}
|
||||
|
||||
// If a complete block was accumulated, write it
|
||||
if (ctx->stored == ctx->partition->erase_size) {
|
||||
if (write_block(ctx->partition, ctx->fd, ctx->buffer)) return -1;
|
||||
ctx->stored = 0;
|
||||
}
|
||||
|
||||
// Write complete blocks directly from the user's buffer
|
||||
while (ctx->stored == 0 && len - wrote >= ctx->partition->erase_size) {
|
||||
if (write_block(ctx->partition, ctx->fd, data + wrote)) return -1;
|
||||
wrote += ctx->partition->erase_size;
|
||||
}
|
||||
}
|
||||
|
||||
return wrote;
|
||||
}
|
||||
|
||||
off_t mtd_erase_blocks(MtdWriteContext *ctx, int blocks)
|
||||
{
|
||||
// Zero-pad and write any pending data to get us to a block boundary
|
||||
if (ctx->stored > 0) {
|
||||
size_t zero = ctx->partition->erase_size - ctx->stored;
|
||||
memset(ctx->buffer + ctx->stored, 0, zero);
|
||||
if (write_block(ctx->partition, ctx->fd, ctx->buffer)) return -1;
|
||||
ctx->stored = 0;
|
||||
}
|
||||
|
||||
off_t pos = lseek(ctx->fd, 0, SEEK_CUR);
|
||||
if ((off_t) pos == (off_t) -1) return pos;
|
||||
|
||||
const int total = (ctx->partition->size - pos) / ctx->partition->erase_size;
|
||||
if (blocks < 0) blocks = total;
|
||||
if (blocks > total) {
|
||||
errno = ENOSPC;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Erase the specified number of blocks
|
||||
while (blocks-- > 0) {
|
||||
loff_t bpos = pos;
|
||||
if (ioctl(ctx->fd, MEMGETBADBLOCK, &bpos) > 0) {
|
||||
fprintf(stderr, "mtd: not erasing bad block at 0x%08lx\n", pos);
|
||||
pos += ctx->partition->erase_size;
|
||||
continue; // Don't try to erase known factory-bad blocks.
|
||||
}
|
||||
|
||||
struct erase_info_user erase_info;
|
||||
erase_info.start = pos;
|
||||
erase_info.length = ctx->partition->erase_size;
|
||||
if (ioctl(ctx->fd, MEMERASE, &erase_info) < 0) {
|
||||
fprintf(stderr, "mtd: erase failure at 0x%08lx\n", pos);
|
||||
}
|
||||
pos += ctx->partition->erase_size;
|
||||
}
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
int mtd_write_close(MtdWriteContext *ctx)
|
||||
{
|
||||
int r = 0;
|
||||
// Make sure any pending data gets written
|
||||
if (mtd_erase_blocks(ctx, 0) == (off_t) -1) r = -1;
|
||||
if (close(ctx->fd)) r = -1;
|
||||
free(ctx->buffer);
|
||||
free(ctx);
|
||||
return r;
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2007 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 MTDUTILS_H_
|
||||
#define MTDUTILS_H_
|
||||
|
||||
#include <sys/types.h> // for size_t, etc.
|
||||
|
||||
typedef struct MtdPartition MtdPartition;
|
||||
|
||||
int mtd_scan_partitions(void);
|
||||
|
||||
const MtdPartition *mtd_find_partition_by_name(const char *name);
|
||||
|
||||
/* mount_point is like "/system"
|
||||
* filesystem is like "yaffs2"
|
||||
*/
|
||||
int mtd_mount_partition(const MtdPartition *partition, const char *mount_point,
|
||||
const char *filesystem, int read_only);
|
||||
|
||||
/* get the partition and the minimum erase/write block size. NULL is ok.
|
||||
*/
|
||||
int mtd_partition_info(const MtdPartition *partition,
|
||||
size_t *total_size, size_t *erase_size, size_t *write_size);
|
||||
|
||||
/* read or write raw data from a partition, starting at the beginning.
|
||||
* skips bad blocks as best we can.
|
||||
*/
|
||||
typedef struct MtdReadContext MtdReadContext;
|
||||
typedef struct MtdWriteContext MtdWriteContext;
|
||||
|
||||
MtdReadContext *mtd_read_partition(const MtdPartition *);
|
||||
ssize_t mtd_read_data(MtdReadContext *, char *data, size_t data_len);
|
||||
ssize_t mtd_read_raw(MtdReadContext *, char *data, size_t data_len);
|
||||
void mtd_read_close(MtdReadContext *);
|
||||
|
||||
MtdWriteContext *mtd_write_partition(const MtdPartition *);
|
||||
ssize_t mtd_write_data(MtdWriteContext *, const char *data, size_t data_len);
|
||||
off_t mtd_erase_blocks(MtdWriteContext *, int blocks); /* 0 ok, -1 for all */
|
||||
int mtd_write_close(MtdWriteContext *);
|
||||
|
||||
#endif // MTDUTILS_H_
|
@ -27,6 +27,25 @@ LOCAL_MODULE_TAGS := eng
|
||||
LOCAL_STATIC_LIBRARIES := libmtdutils libcutils libc
|
||||
LOCAL_MODULE_STEM := flash_image
|
||||
LOCAL_FORCE_STATIC_EXECUTABLE := true
|
||||
LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
|
||||
ADDITIONAL_RECOVERY_EXECUTABLES += recovery_flash_image
|
||||
include $(BUILD_EXECUTABLE)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_SRC_FILES := dump_image.c mtdutils.c mounts.c
|
||||
LOCAL_MODULE := recovery_dump_image
|
||||
LOCAL_MODULE_TAGS := eng
|
||||
LOCAL_STATIC_LIBRARIES := libcutils libc
|
||||
LOCAL_MODULE_STEM := dump_image
|
||||
LOCAL_FORCE_STATIC_EXECUTABLE := true
|
||||
LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
|
||||
ADDITIONAL_RECOVERY_EXECUTABLES += recovery_dump_image
|
||||
include $(BUILD_EXECUTABLE)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_SRC_FILES := dump_image.c mtdutils.c mounts.c
|
||||
LOCAL_MODULE := dump_image
|
||||
LOCAL_MODULE_TAGS := eng
|
||||
include $(BUILD_EXECUTABLE)
|
||||
|
||||
endif # TARGET_ARCH == arm
|
||||
|
@ -38,6 +38,7 @@ struct MtdPartition {
|
||||
struct MtdReadContext {
|
||||
const MtdPartition *partition;
|
||||
char *buffer;
|
||||
size_t read_size;
|
||||
size_t consumed;
|
||||
int fd;
|
||||
};
|
||||
@ -564,3 +565,84 @@ off_t mtd_find_write_start(MtdWriteContext *ctx, off_t pos) {
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
|
||||
ssize_t mtd_read_raw(MtdReadContext *ctx, char *data, size_t len)
|
||||
{
|
||||
static const int SPARE_SIZE = (2048 >> 5);
|
||||
struct mtd_info_user mtd_info;
|
||||
struct mtd_oob_buf oob_buf;
|
||||
struct nand_ecclayout ecc_layout;
|
||||
struct nand_oobfree *fp;
|
||||
unsigned char ecc[SPARE_SIZE];
|
||||
char *src, *dst;
|
||||
int i, n, ret;
|
||||
|
||||
/*
|
||||
* FIXME: These two ioctls should be cached in MtdReadContext.
|
||||
*/
|
||||
ret = ioctl(ctx->fd, MEMGETINFO, &mtd_info);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
ret = ioctl(ctx->fd, ECCGETLAYOUT, &ecc_layout);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
ctx->read_size = mtd_info.writesize;
|
||||
ctx->consumed = ctx->read_size;
|
||||
|
||||
/*
|
||||
* Read next good data block.
|
||||
*/
|
||||
ret = read_block(ctx->partition, ctx->fd, data);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
dst = src = data + ctx->read_size;
|
||||
|
||||
/*
|
||||
* Read OOB data for last block read in read_block().
|
||||
*/
|
||||
oob_buf.start = lseek(ctx->fd, 0, SEEK_CUR) - ctx->read_size;
|
||||
oob_buf.length = mtd_info.oobsize;
|
||||
oob_buf.ptr = (unsigned char *) src;
|
||||
|
||||
ret = ioctl(ctx->fd, MEMREADOOB, &oob_buf);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
/*
|
||||
* As yaffs and yaffs2 use mode MEM_OOB_AUTO, but mtdchar uses
|
||||
* MEM_OOB_PLACE, copy the spare data down the hard way.
|
||||
*
|
||||
* Safe away ECC data:
|
||||
*/
|
||||
for (i = 0; i < ecc_layout.eccbytes; i++) {
|
||||
ecc[i] = src[ecc_layout.eccpos[i]];
|
||||
}
|
||||
for ( ; i < SPARE_SIZE; i++) {
|
||||
ecc[i] = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy yaffs2 spare data down.
|
||||
*/
|
||||
n = ecc_layout.oobavail;
|
||||
fp = &ecc_layout.oobfree[0];
|
||||
while (n) {
|
||||
if (fp->offset) {
|
||||
memmove(dst, src + fp->offset, fp->length);
|
||||
}
|
||||
dst += fp->length;
|
||||
n -= fp->length;
|
||||
++fp;
|
||||
}
|
||||
|
||||
/*
|
||||
* Restore ECC data behind spare data.
|
||||
*/
|
||||
memcpy(dst, ecc, (ctx->read_size >> 5) - ecc_layout.oobavail);
|
||||
|
||||
return ctx->read_size + (ctx->read_size >> 5);
|
||||
}
|
||||
|
@ -52,4 +52,6 @@ off_t mtd_erase_blocks(MtdWriteContext *, int blocks); /* 0 ok, -1 for all */
|
||||
off_t mtd_find_write_start(MtdWriteContext *ctx, off_t pos);
|
||||
int mtd_write_close(MtdWriteContext *);
|
||||
|
||||
ssize_t mtd_read_raw(MtdReadContext *ctx, char *data, size_t len);
|
||||
|
||||
#endif // MTDUTILS_H_
|
||||
|
24
nandroid/Android.mk
Normal file
24
nandroid/Android.mk
Normal file
@ -0,0 +1,24 @@
|
||||
ifneq ($(TARGET_SIMULATOR),true)
|
||||
ifeq ($(TARGET_ARCH),arm)
|
||||
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := recovery_nandroid
|
||||
LOCAL_MODULE_CLASS := EXECUTABLES
|
||||
LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
|
||||
LOCAL_SRC_FILES := nandroid-mobile.sh
|
||||
LOCAL_MODULE_STEM := nandroid-mobile.sh
|
||||
ADDITIONAL_RECOVERY_EXECUTABLES += recovery_nandroid
|
||||
include $(BUILD_PREBUILT)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := recovery_unyaffs
|
||||
LOCAL_MODULE_STEM := unyaffs
|
||||
LOCAL_FORCE_STATIC_EXECUTABLE := true
|
||||
LOCAL_SRC_FILES := unyaffs.c
|
||||
LOCAL_STATIC_LIBRARIES := libc libcutils
|
||||
LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
|
||||
include $(BUILD_EXECUTABLE)
|
||||
|
||||
endif # TARGET_ARCH == arm
|
||||
endif # !TARGET_SIMULATOR
|
1790
nandroid/nandroid-mobile.sh
Executable file
1790
nandroid/nandroid-mobile.sh
Executable file
File diff suppressed because it is too large
Load Diff
118
nandroid/unyaffs.c
Normal file
118
nandroid/unyaffs.c
Normal file
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* unyaffs: extract files from yaffs2 file system image to current directory
|
||||
*
|
||||
* Created by Kai Wei <kai.wei.cn@gmail.com>
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "unyaffs.h"
|
||||
|
||||
#define CHUNK_SIZE 2048
|
||||
#define SPARE_SIZE 64
|
||||
#define MAX_OBJECTS 10000
|
||||
#define YAFFS_OBJECTID_ROOT 1
|
||||
|
||||
|
||||
unsigned char data[CHUNK_SIZE + SPARE_SIZE];
|
||||
unsigned char *chunk_data = data;
|
||||
unsigned char *spare_data = data + CHUNK_SIZE;
|
||||
int img_file;
|
||||
|
||||
char *obj_list[MAX_OBJECTS];
|
||||
void process_chunk()
|
||||
{
|
||||
int out_file, remain, s;
|
||||
char *full_path_name;
|
||||
|
||||
yaffs_PackedTags2 *pt = (yaffs_PackedTags2 *)spare_data;
|
||||
if (pt->t.byteCount == 0xffff) { //a new object
|
||||
|
||||
yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)malloc(sizeof(yaffs_ObjectHeader));
|
||||
memcpy(oh, chunk_data, sizeof(yaffs_ObjectHeader));
|
||||
|
||||
full_path_name = (char *)malloc(strlen(oh->name) + strlen(obj_list[oh->parentObjectId]) + 2);
|
||||
if (full_path_name == NULL) {
|
||||
perror("malloc full path name\n");
|
||||
}
|
||||
strcpy(full_path_name, obj_list[oh->parentObjectId]);
|
||||
strcat(full_path_name, "/");
|
||||
strcat(full_path_name, oh->name);
|
||||
obj_list[pt->t.objectId] = full_path_name;
|
||||
|
||||
switch(oh->type) {
|
||||
case YAFFS_OBJECT_TYPE_FILE:
|
||||
remain = oh->fileSize;
|
||||
out_file = creat(full_path_name, oh->yst_mode);
|
||||
while(remain > 0) {
|
||||
if (read_chunk())
|
||||
return -1;
|
||||
s = (remain < pt->t.byteCount) ? remain : pt->t.byteCount;
|
||||
if (write(out_file, chunk_data, s) == -1)
|
||||
return -1;
|
||||
remain -= s;
|
||||
}
|
||||
close(out_file);
|
||||
break;
|
||||
case YAFFS_OBJECT_TYPE_SYMLINK:
|
||||
symlink(oh->alias, full_path_name);
|
||||
break;
|
||||
case YAFFS_OBJECT_TYPE_DIRECTORY:
|
||||
mkdir(full_path_name, 0777);
|
||||
break;
|
||||
case YAFFS_OBJECT_TYPE_HARDLINK:
|
||||
link(obj_list[oh->equivalentObjectId], full_path_name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int read_chunk()
|
||||
{
|
||||
ssize_t s;
|
||||
int ret = -1;
|
||||
memset(chunk_data, 0xff, sizeof(chunk_data));
|
||||
s = read(img_file, data, CHUNK_SIZE + SPARE_SIZE);
|
||||
if (s == -1) {
|
||||
perror("read image file\n");
|
||||
} else if (s == 0) {
|
||||
printf("end of image\n");
|
||||
} else if ((s == (CHUNK_SIZE + SPARE_SIZE))) {
|
||||
ret = 0;
|
||||
} else {
|
||||
fprintf(stderr, "broken image file\n");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
printf("Usage: unyaffs image_file_name\n");
|
||||
exit(1);
|
||||
}
|
||||
img_file = open(argv[1], O_RDONLY);
|
||||
if (img_file == -1) {
|
||||
printf("open image file failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
obj_list[YAFFS_OBJECTID_ROOT] = ".";
|
||||
while(1) {
|
||||
if (read_chunk() == -1)
|
||||
break;
|
||||
process_chunk();
|
||||
}
|
||||
close(img_file);
|
||||
return 0;
|
||||
}
|
144
nandroid/unyaffs.h
Normal file
144
nandroid/unyaffs.h
Normal file
@ -0,0 +1,144 @@
|
||||
/*
|
||||
* definition copied from yaffs project
|
||||
*/
|
||||
|
||||
#ifndef __UNYAFFS_H__
|
||||
#define __UNYAFFS_H__
|
||||
|
||||
|
||||
#define YAFFS_MAX_NAME_LENGTH 255
|
||||
#define YAFFS_MAX_ALIAS_LENGTH 159
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
/* Definition of types */
|
||||
#ifndef __ASM_ARM_TYPES_H
|
||||
typedef unsigned char __u8;
|
||||
typedef unsigned short __u16;
|
||||
typedef unsigned __u32;
|
||||
#endif
|
||||
typedef struct {
|
||||
unsigned sequenceNumber;
|
||||
unsigned objectId;
|
||||
unsigned chunkId;
|
||||
unsigned byteCount;
|
||||
} yaffs_PackedTags2TagsPart;
|
||||
|
||||
typedef struct {
|
||||
unsigned char colParity;
|
||||
unsigned lineParity;
|
||||
unsigned lineParityPrime;
|
||||
} yaffs_ECCOther;
|
||||
|
||||
typedef struct {
|
||||
yaffs_PackedTags2TagsPart t;
|
||||
yaffs_ECCOther ecc;
|
||||
} yaffs_PackedTags2;
|
||||
|
||||
typedef enum {
|
||||
YAFFS_ECC_RESULT_UNKNOWN,
|
||||
YAFFS_ECC_RESULT_NO_ERROR,
|
||||
YAFFS_ECC_RESULT_FIXED,
|
||||
YAFFS_ECC_RESULT_UNFIXED
|
||||
} yaffs_ECCResult;
|
||||
|
||||
typedef enum {
|
||||
YAFFS_OBJECT_TYPE_UNKNOWN,
|
||||
YAFFS_OBJECT_TYPE_FILE,
|
||||
YAFFS_OBJECT_TYPE_SYMLINK,
|
||||
YAFFS_OBJECT_TYPE_DIRECTORY,
|
||||
YAFFS_OBJECT_TYPE_HARDLINK,
|
||||
YAFFS_OBJECT_TYPE_SPECIAL
|
||||
} yaffs_ObjectType;
|
||||
|
||||
|
||||
typedef struct {
|
||||
|
||||
unsigned validMarker0;
|
||||
unsigned chunkUsed; /* Status of the chunk: used or unused */
|
||||
unsigned objectId; /* If 0 then this is not part of an object (unused) */
|
||||
unsigned chunkId; /* If 0 then this is a header, else a data chunk */
|
||||
unsigned byteCount; /* Only valid for data chunks */
|
||||
|
||||
/* The following stuff only has meaning when we read */
|
||||
yaffs_ECCResult eccResult;
|
||||
unsigned blockBad;
|
||||
|
||||
/* YAFFS 1 stuff */
|
||||
unsigned chunkDeleted; /* The chunk is marked deleted */
|
||||
unsigned serialNumber; /* Yaffs1 2-bit serial number */
|
||||
|
||||
/* YAFFS2 stuff */
|
||||
unsigned sequenceNumber; /* The sequence number of this block */
|
||||
|
||||
/* Extra info if this is an object header (YAFFS2 only) */
|
||||
|
||||
unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
|
||||
unsigned extraParentObjectId; /* The parent object */
|
||||
unsigned extraIsShrinkHeader; /* Is it a shrink header? */
|
||||
unsigned extraShadows; /* Does this shadow another object? */
|
||||
|
||||
yaffs_ObjectType extraObjectType; /* What object type? */
|
||||
|
||||
unsigned extraFileLength; /* Length if it is a file */
|
||||
unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
|
||||
|
||||
unsigned validMarker1;
|
||||
|
||||
} yaffs_ExtendedTags;
|
||||
|
||||
/* -------------------------- Object structure -------------------------------*/
|
||||
/* This is the object structure as stored on NAND */
|
||||
|
||||
typedef struct {
|
||||
yaffs_ObjectType type;
|
||||
|
||||
/* Apply to everything */
|
||||
int parentObjectId;
|
||||
__u16 sum__NoLongerUsed; /* checksum of name. No longer used */
|
||||
char name[YAFFS_MAX_NAME_LENGTH + 1];
|
||||
|
||||
/* The following apply to directories, files, symlinks - not hard links */
|
||||
__u32 yst_mode; /* protection */
|
||||
|
||||
#ifdef CONFIG_YAFFS_WINCE
|
||||
__u32 notForWinCE[5];
|
||||
#else
|
||||
__u32 yst_uid;
|
||||
__u32 yst_gid;
|
||||
__u32 yst_atime;
|
||||
__u32 yst_mtime;
|
||||
__u32 yst_ctime;
|
||||
#endif
|
||||
|
||||
/* File size applies to files only */
|
||||
int fileSize;
|
||||
|
||||
/* Equivalent object id applies to hard links only. */
|
||||
int equivalentObjectId;
|
||||
|
||||
/* Alias is for symlinks only. */
|
||||
char alias[YAFFS_MAX_ALIAS_LENGTH + 1];
|
||||
|
||||
__u32 yst_rdev; /* device stuff for block and char devices (major/min) */
|
||||
|
||||
#ifdef CONFIG_YAFFS_WINCE
|
||||
__u32 win_ctime[2];
|
||||
__u32 win_atime[2];
|
||||
__u32 win_mtime[2];
|
||||
#else
|
||||
__u32 roomToGrow[6];
|
||||
|
||||
#endif
|
||||
__u32 inbandShadowsObject;
|
||||
__u32 inbandIsShrink;
|
||||
|
||||
__u32 reservedSpace[2];
|
||||
int shadowsObject; /* This object header shadows the specified object if > 0 */
|
||||
|
||||
/* isShrink applies to object headers written when we shrink the file (ie resize) */
|
||||
__u32 isShrink;
|
||||
|
||||
} yaffs_ObjectHeader;
|
||||
|
||||
#endif
|
@ -1,293 +0,0 @@
|
||||
#!/sbin/sh
|
||||
|
||||
# nandroid v2.1 - an Android backup tool for the G1 by infernix and brainaid
|
||||
|
||||
# Requirements:
|
||||
|
||||
# - a modded android in recovery mode (JF 1.3 will work by default)
|
||||
# - adb shell as root in recovery mode if not using a pre-made recovery image
|
||||
# - busybox in recovery mode
|
||||
# - dump_image-arm-uclibc compiled and in path on phone
|
||||
# - mkyaffs2image-arm-uclibc compiled and installed in path on phone
|
||||
|
||||
# Reference data:
|
||||
|
||||
# dev: size erasesize name
|
||||
#mtd0: 00040000 00020000 "misc"
|
||||
#mtd1: 00500000 00020000 "recovery"
|
||||
#mtd2: 00280000 00020000 "boot"
|
||||
#mtd3: 04380000 00020000 "system"
|
||||
#mtd4: 04380000 00020000 "cache"
|
||||
#mtd5: 04ac0000 00020000 "userdata"
|
||||
#mtd6 is everything, dump splash1 with: dd if=/dev/mtd/mtd6ro of=/sdcard/splash1.img skip=19072 bs=2048 count=150
|
||||
|
||||
# We don't dump misc or cache because they do not contain any useful data that we are aware of at this time.
|
||||
|
||||
# Logical steps (v2.1):
|
||||
#
|
||||
# 0. test for a target dir and the various tools needed, if not found then exit with error.
|
||||
# 1. check "adb devices" for a device in recovery mode. set DEVICEID variable to the device ID. abort when not found.
|
||||
# 2. mount system and data partitions read-only, set up adb portforward and create destdir
|
||||
# 3. check free space on /cache, exit if less blocks than 20MB free
|
||||
# 4. push required tools to device in /cache
|
||||
# 5 for partitions boot recovery misc:
|
||||
# 5a get md5sum for content of current partition *on the device* (no data transfered)
|
||||
# 5b while MD5sum comparison is incorrect (always is the first time):
|
||||
# 5b1 dump current partition to a netcat session
|
||||
# 5b2 start local netcat to dump image to current dir
|
||||
# 5b3 compare md5sums of dumped data with dump in current dir. if correct, contine, else restart the loop (6b1)
|
||||
# 6 for partitions system data:
|
||||
# 6a get md5sum for tar of content of current partition *on the device* (no data transfered)
|
||||
# 6b while MD5sum comparison is incorrect (always is the first time):
|
||||
# 6b1 tar current partition to a netcat session
|
||||
# 6b2 start local netcat to dump tar to current dir
|
||||
# 6b3 compare md5sums of dumped data with dump in current dir. if correct, contine, else restart the loop (6b1)
|
||||
# 6c if i'm running as root:
|
||||
# 6c1 create a temp dir using either tempdir command or the deviceid in /tmp
|
||||
# 6c2 extract tar to tempdir
|
||||
# 6c3 invoke mkyaffs2image to create the img
|
||||
# 6c4 clean up
|
||||
# 7. remove tools from device /cache
|
||||
# 8. umount system and data on device
|
||||
# 9. print success.
|
||||
|
||||
|
||||
DEVICEID=foo
|
||||
RECOVERY=foo
|
||||
|
||||
echo "nandroid-mobile v2.1"
|
||||
|
||||
|
||||
if [ "$1" == "" ]; then
|
||||
echo "Usage: $0 {backup|restore} [/path/to/nandroid/backup/]"
|
||||
echo "- backup will store a full system backup on /sdcard/nandroid/$DEVICEID"
|
||||
echo "- restore path will restore the last made backup for boot, system, recovery and data"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
case $1 in
|
||||
backup)
|
||||
mkyaffs2image=`which mkyaffs2image`
|
||||
if [ "$mkyaffs2image" == "" ]; then
|
||||
mkyaffs2image=`which mkyaffs2image-arm-uclibc`
|
||||
if [ "$mkyaffs2image" == "" ]; then
|
||||
echo "error: mkyaffs2image or mkyaffs2image-arm-uclibc not found in path"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
dump_image=`which dump_image`
|
||||
if [ "$dump_image" == "" ]; then
|
||||
dump_image=`which dump_image-arm-uclibc`
|
||||
if [ "$dump_image" == "" ]; then
|
||||
echo "error: dump_image or dump_image-arm-uclibc not found in path"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
break
|
||||
;;
|
||||
restore)
|
||||
flash_image=`which flash_image`
|
||||
if [ "$flash_image" == "" ]; then
|
||||
flash_image=`which flash_image-arm-uclibc`
|
||||
if [ "$flash_image" == "" ]; then
|
||||
echo "error: flash_image or flash_image-arm-uclibc not found in path"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
break
|
||||
;;
|
||||
esac
|
||||
|
||||
# 1
|
||||
DEVICEID=`cat /proc/cmdline | sed "s/.*serialno=//" | cut -d" " -f1`
|
||||
RECOVERY=`cat /proc/cmdline | grep "androidboot.mode=recovery"`
|
||||
if [ "$RECOVERY" == "foo" ]; then
|
||||
echo "error: not running in recovery mode, aborting"
|
||||
exit 1
|
||||
fi
|
||||
if [ "$DEVICEID" == "foo" ]; then
|
||||
echo "error: device id not found in /proc/cmdline, aborting"
|
||||
exit 1
|
||||
fi
|
||||
if [ ! "`id -u 2>/dev/null`" == "0" ]; then
|
||||
if [ "`whoami 2>&1 | grep 'uid 0'`" == "" ]; then
|
||||
echo "error: must run as root, aborting"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
case $1 in
|
||||
restore)
|
||||
ENERGY=`cat /sys/class/power_supply/battery/capacity`
|
||||
if [ "`cat /sys/class/power_supply/battery/status`" == "Charging" ]; then
|
||||
ENERGY=100
|
||||
fi
|
||||
if [ ! $ENERGY -ge 30 ]; then
|
||||
echo "Error: not enough battery power"
|
||||
echo "Connect charger or USB power and try again"
|
||||
exit 1
|
||||
fi
|
||||
RESTOREPATH=$2
|
||||
if [ ! -f $RESTOREPATH/nandroid.md5 ]; then
|
||||
echo "error: $RESTOREPATH/nandroid.md5 not found, cannot verify backup data"
|
||||
exit 1
|
||||
fi
|
||||
umount /system 2>/dev/null
|
||||
umount /data 2>/dev/null
|
||||
if [ ! "`mount | grep data`" == "" ]; then
|
||||
echo "error: unable to umount /data, aborting"
|
||||
exit 1
|
||||
fi
|
||||
if [ ! "`mount | grep system`" == "" ]; then
|
||||
echo "error: unable to umount /system, aborting"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Verifying backup images..."
|
||||
CWD=$PWD
|
||||
cd $RESTOREPATH
|
||||
md5sum -c nandroid.md5
|
||||
if [ $? -eq 1 ]; then
|
||||
echo "error: md5sum mismatch, aborting"
|
||||
exit 1
|
||||
fi
|
||||
for image in boot recovery; do
|
||||
echo "Flashing $image..."
|
||||
$flash_image $image $image.img
|
||||
done
|
||||
echo "Flashing system and data not currently supported"
|
||||
echo "Restore done"
|
||||
exit 0
|
||||
;;
|
||||
backup)
|
||||
break
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {backup|restore} [/path/to/nandroid/backup/]"
|
||||
echo "- backup will store a full system backup on /sdcard/nandroid/$DEVICEID"
|
||||
echo "- restore path will restore the last made backup for boot, system, recovery and data"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# 2.
|
||||
echo "mounting system and data read-only, sdcard read-write"
|
||||
umount /system 2>/dev/null
|
||||
umount /data 2>/dev/null
|
||||
umount /sdcard 2>/dev/null
|
||||
mount -o ro /system || FAIL=1
|
||||
mount -o ro /data || FAIL=2
|
||||
mount /sdcard || mount /dev/block/mmcblk0 /sdcard || FAIL=3
|
||||
case $FAIL in
|
||||
1) echo "Error mounting system read-only"; umount /system /data /sdcard; exit 1;;
|
||||
2) echo "Error mounting data read-only"; umount /system /data /sdcard; exit 1;;
|
||||
3) echo "Error mounting sdcard read-write"; umount /system /data /sdcard; exit 1;;
|
||||
esac
|
||||
|
||||
TIMESTAMP="`date +%Y%m%d-%H%M`"
|
||||
DESTDIR="/sdcard/nandroid/$DEVICEID/$TIMESTAMP"
|
||||
if [ ! -d $DESTDIR ]; then
|
||||
mkdir -p $DESTDIR
|
||||
if [ ! -d $DESTDIR ]; then
|
||||
echo "error: cannot create $DESTDIR"
|
||||
umount /system 2>/dev/null
|
||||
umount /data 2>/dev/null
|
||||
umount /sdcard 2>/dev/null
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
touch $DESTDIR/.nandroidwritable
|
||||
if [ ! -e $DESTDIR/.nandroidwritable ]; then
|
||||
echo "error: cannot write to $DESTDIR"
|
||||
umount /system 2>/dev/null
|
||||
umount /data 2>/dev/null
|
||||
umount /sdcard 2>/dev/null
|
||||
exit 1
|
||||
fi
|
||||
rm $DESTDIR/.nandroidwritable
|
||||
fi
|
||||
|
||||
# 3.
|
||||
echo "checking free space on sdcard"
|
||||
FREEBLOCKS="`df -k /sdcard| grep sdcard | awk '{ print $4 }'`"
|
||||
# we need about 130MB for the dump
|
||||
if [ $FREEBLOCKS -le 130000 ]; then
|
||||
echo "error: not enough free space available on sdcard (need 130mb), aborting."
|
||||
umount /system 2>/dev/null
|
||||
umount /data 2>/dev/null
|
||||
umount /sdcard 2>/dev/null
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
|
||||
if [ -e /dev/mtd/mtd6ro ]; then
|
||||
echo -n "Dumping splash1 from device over tcp to $DESTDIR/splash1.img..."
|
||||
dd if=/dev/mtd/mtd6ro of=$DESTDIR/splash1.img skip=19072 bs=2048 count=150 2>/dev/null
|
||||
echo "done"
|
||||
sleep 1s
|
||||
echo -n "Dumping splash2 from device over tcp to $DESTDIR/splash2.img..."
|
||||
dd if=/dev/mtd/mtd6ro of=$DESTDIR/splash2.img skip=19456 bs=2048 count=150 2>/dev/null
|
||||
echo "done"
|
||||
fi
|
||||
|
||||
|
||||
# 5.
|
||||
for image in boot recovery misc; do
|
||||
# 5a
|
||||
DEVICEMD5=`$dump_image $image - | md5sum | awk '{ print $1 }'`
|
||||
sleep 1s
|
||||
MD5RESULT=1
|
||||
# 5b
|
||||
echo -n "Dumping $image to $DESTDIR/$image.img..."
|
||||
ATTEMPT=0
|
||||
while [ $MD5RESULT -eq 1 ]; do
|
||||
let ATTEMPT=$ATTEMPT+1
|
||||
# 5b1
|
||||
$dump_image $image $DESTDIR/$image.img
|
||||
sync
|
||||
# 5b3
|
||||
echo "${DEVICEMD5} $DESTDIR/$image.img" | md5sum -c -s -
|
||||
if [ $? -eq 1 ]; then
|
||||
true
|
||||
else
|
||||
MD5RESULT=0
|
||||
fi
|
||||
if [ "$ATTEMPT" == "5" ]; then
|
||||
echo "fatal error while trying to dump $image, aborting"
|
||||
umount /system
|
||||
umount /data
|
||||
umount /sdcard
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
echo "done"
|
||||
done
|
||||
|
||||
# 6
|
||||
for image in system data cache; do
|
||||
# 6a
|
||||
echo -n "Dumping $image to $DESTDIR/$image.img..."
|
||||
$mkyaffs2image /$image $DESTDIR/$image.img
|
||||
sync
|
||||
echo "done"
|
||||
done
|
||||
|
||||
|
||||
# 7.
|
||||
echo -n "generating md5sum file..."
|
||||
CWD=$PWD
|
||||
cd $DESTDIR
|
||||
md5sum *img > nandroid.md5
|
||||
cd $CWD
|
||||
echo "done"
|
||||
|
||||
# 8.
|
||||
echo "unmounting system, data and sdcard"
|
||||
umount /system
|
||||
umount /data
|
||||
umount /sdcard
|
||||
|
||||
# 9.
|
||||
echo "Backup successful."
|
Loading…
Reference in New Issue
Block a user