From 815ca5d230e2416370ab9645c0da9e4a0905000d Mon Sep 17 00:00:00 2001 From: Shashank Mittal Date: Tue, 27 Jul 2010 11:09:19 -0700 Subject: [recovery]: Add support for OTA upgrade on mmc devices. Change-Id: I8f230dfa5be4e9f142765797d949e10434e1fdeb --- mmcutils/Android.mk | 15 ++ mmcutils/mmcutils.c | 415 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mmcutils/mmcutils.h | 85 +++++++++++ 3 files changed, 515 insertions(+) create mode 100644 mmcutils/Android.mk create mode 100644 mmcutils/mmcutils.c create mode 100644 mmcutils/mmcutils.h (limited to 'mmcutils') diff --git a/mmcutils/Android.mk b/mmcutils/Android.mk new file mode 100644 index 0000000..36bd515 --- /dev/null +++ b/mmcutils/Android.mk @@ -0,0 +1,15 @@ +ifneq ($(TARGET_SIMULATOR),true) +ifeq ($(TARGET_ARCH),arm) + +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + mmcutils.c + +LOCAL_MODULE := libmmcutils + +include $(BUILD_STATIC_LIBRARY) + +endif # TARGET_ARCH == arm +endif # !TARGET_SIMULATOR diff --git a/mmcutils/mmcutils.c b/mmcutils/mmcutils.c new file mode 100644 index 0000000..d7e459a --- /dev/null +++ b/mmcutils/mmcutils.c @@ -0,0 +1,415 @@ +/* + * Copyright (c) 2010, Code Aurora Forum. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of Code Aurora Forum, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // for _IOW, _IOR, mount() + +#include "mmcutils.h" + +unsigned ext3_count = 0; +char *ext3_partitions[] = {"system", "userdata", "cache", "NONE"}; + +unsigned vfat_count = 0; +char *vfat_partitions[] = {"modem", "NONE"}; + +struct MmcPartition { + char *device_index; + char *filesystem; + char *name; + unsigned dstatus; + unsigned dtype ; + unsigned dfirstsec; + unsigned dsize; +}; + +typedef struct { + MmcPartition *partitions; + int partitions_allocd; + int partition_count; +} MmcState; + +static MmcState g_mmc_state = { + NULL, // partitions + 0, // partitions_allocd + -1 // partition_count +}; + +#define MMC_DEVICENAME "/dev/block/mmcblk0" + +static void +mmc_partition_name (MmcPartition *mbr, unsigned int type) { + switch(type) + { + char name[64]; + case MMC_BOOT_TYPE: + sprintf(name,"boot"); + mbr->name = strdup(name); + break; + case MMC_EXT3_TYPE: + if (strcmp("NONE", ext3_partitions[ext3_count])) { + strcpy((char *)name,(const char *)ext3_partitions[ext3_count]); + mbr->name = strdup(name); + ext3_count++; + } + mbr->filesystem = strdup("ext3"); + break; + case MMC_VFAT_TYPE: + if (strcmp("NONE", vfat_partitions[vfat_count])) { + strcpy((char *)name,(const char *)vfat_partitions[vfat_count]); + mbr->name = strdup(name); + vfat_count++; + } + mbr->filesystem = strdup("vfat"); + break; + }; +} + +static int +mmc_read_mbr (const char *device, MmcPartition *mbr) { + FILE *fd; + unsigned char buffer[512]; + int idx, i; + unsigned mmc_partition_count = 0; + unsigned int dtype; + unsigned int dfirstsec; + unsigned int EBR_first_sec; + unsigned int EBR_current_sec; + int ret = -1; + + fd = fopen(device, "r"); + if(fd == NULL) + { + printf("Can't open device: \"%s\"\n", device); + goto ERROR2; + } + if ((fread(buffer, 512, 1, fd)) != 1) + { + printf("Can't read device: \"%s\"\n", device); + goto ERROR1; + } + /* Check to see if signature exists */ + if ((buffer[TABLE_SIGNATURE] != 0x55) || \ + (buffer[TABLE_SIGNATURE + 1] != 0xAA)) + { + printf("Incorrect mbr signatures!\n"); + goto ERROR1; + } + idx = TABLE_ENTRY_0; + for (i = 0; i < 4; i++) + { + char device_index[128]; + + mbr[mmc_partition_count].dstatus = \ + buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_STATUS]; + mbr[mmc_partition_count].dtype = \ + buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE]; + mbr[mmc_partition_count].dfirstsec = \ + GET_LWORD_FROM_BYTE(&buffer[idx + \ + i * TABLE_ENTRY_SIZE + \ + OFFSET_FIRST_SEC]); + mbr[mmc_partition_count].dsize = \ + GET_LWORD_FROM_BYTE(&buffer[idx + \ + i * TABLE_ENTRY_SIZE + \ + OFFSET_SIZE]); + dtype = mbr[mmc_partition_count].dtype; + dfirstsec = mbr[mmc_partition_count].dfirstsec; + mmc_partition_name(&mbr[mmc_partition_count], \ + mbr[mmc_partition_count].dtype); + + sprintf(device_index, "%sp%d", device, (mmc_partition_count+1)); + mbr[mmc_partition_count].device_index = strdup(device_index); + + mmc_partition_count++; + if (mmc_partition_count == MAX_PARTITIONS) + goto SUCCESS; + } + + /* See if the last partition is EBR, if not, parsing is done */ + if (dtype != 0x05) + { + goto SUCCESS; + } + + EBR_first_sec = dfirstsec; + EBR_current_sec = dfirstsec; + + fseek (fd, (EBR_first_sec * 512), SEEK_SET); + if ((fread(buffer, 512, 1, fd)) != 1) + goto ERROR1; + + /* Loop to parse the EBR */ + for (i = 0;; i++) + { + char device_index[128]; + + if ((buffer[TABLE_SIGNATURE] != 0x55) || (buffer[TABLE_SIGNATURE + 1] != 0xAA)) + { + break; + } + mbr[mmc_partition_count].dstatus = \ + buffer[TABLE_ENTRY_0 + OFFSET_STATUS]; + mbr[mmc_partition_count].dtype = \ + buffer[TABLE_ENTRY_0 + OFFSET_TYPE]; + mbr[mmc_partition_count].dfirstsec = \ + GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + \ + OFFSET_FIRST_SEC]) + \ + EBR_current_sec; + mbr[mmc_partition_count].dsize = \ + GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + \ + OFFSET_SIZE]); + mmc_partition_name(&mbr[mmc_partition_count], \ + mbr[mmc_partition_count].dtype); + + sprintf(device_index, "%sp%d", device, (mmc_partition_count+1)); + mbr[mmc_partition_count].device_index = strdup(device_index); + + mmc_partition_count++; + if (mmc_partition_count == MAX_PARTITIONS) + goto SUCCESS; + + dfirstsec = GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_1 + OFFSET_FIRST_SEC]); + if(dfirstsec == 0) + { + /* Getting to the end of the EBR tables */ + break; + } + /* More EBR to follow - read in the next EBR sector */ + fseek (fd, ((EBR_first_sec + dfirstsec) * 512), SEEK_SET); + if ((fread(buffer, 512, 1, fd)) != 1) + goto ERROR1; + + EBR_current_sec = EBR_first_sec + dfirstsec; + } + +SUCCESS: + ret = mmc_partition_count; +ERROR1: + fclose(fd); +ERROR2: + return ret; +} + +int +mmc_scan_partitions() { + int i; + ssize_t nbytes; + + if (g_mmc_state.partitions == NULL) { + const int nump = MAX_PARTITIONS; + MmcPartition *partitions = malloc(nump * sizeof(*partitions)); + if (partitions == NULL) { + errno = ENOMEM; + return -1; + } + g_mmc_state.partitions = partitions; + g_mmc_state.partitions_allocd = nump; + memset(partitions, 0, nump * sizeof(*partitions)); + } + g_mmc_state.partition_count = 0; + ext3_count = 0; + vfat_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_mmc_state.partitions_allocd; i++) { + MmcPartition *p = &g_mmc_state.partitions[i]; + if (p->device_index != NULL) { + free(p->device_index); + p->device_index = NULL; + } + if (p->name != NULL) { + free(p->name); + p->name = NULL; + } + if (p->filesystem != NULL) { + free(p->filesystem); + p->filesystem = NULL; + } + } + + g_mmc_state.partition_count = mmc_read_mbr(MMC_DEVICENAME, g_mmc_state.partitions); + if(g_mmc_state.partition_count == -1) + { + printf("Error in reading mbr!\n"); + // keep "partitions" around so we can free the names on a rescan. + g_mmc_state.partition_count = -1; + } + return g_mmc_state.partition_count; +} + +const MmcPartition * +mmc_find_partition_by_name(const char *name) +{ + if (g_mmc_state.partitions != NULL) { + int i; + for (i = 0; i < g_mmc_state.partitions_allocd; i++) { + MmcPartition *p = &g_mmc_state.partitions[i]; + if (p->device_index !=NULL && p->name != NULL) { + if (strcmp(p->name, name) == 0) { + return p; + } + } + } + } + return NULL; +} + +#define MKE2FS_BIN "/sbin/mke2fs_static" +#define TUNE2FS_BIN "/sbin/tune2fs_static" +#define E2FSCK_BIN "/sbin/e2fsck_static" + +static int +run_exec_process ( char **argv) { + pid_t pid; + int status; + pid = fork(); + if (pid == 0) { + execv(argv[0], argv); + fprintf(stderr, "E:Can't run (%s)\n",strerror(errno)); + _exit(-1); + } + + waitpid(pid, &status, 0); + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { + return 1; + } + return 0; +} + +int +mmc_format_ext3 (MmcPartition *partition) { + char device[128]; + strcpy(device, partition->device_index); + // Run mke2fs + char *const mke2fs[] = {MKE2FS_BIN, "-j", device, NULL}; + if(run_exec_process(mke2fs)) + return -1; + + // Run tune2fs + char *const tune2fs[] = {TUNE2FS_BIN, "-C", "1", device, NULL}; + if(run_exec_process(tune2fs)) + return -1; + + // Run e2fsck + char *const e2fsck[] = {E2FSCK_BIN, "-fy", device, NULL}; + if(run_exec_process(e2fsck)) + return -1; + + return 0; +} + +int +mmc_mount_partition(const MmcPartition *partition, const char *mount_point, + int read_only) +{ + const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME; + char devname[128]; + int rv = -1; + strcpy(devname, partition->device_index); + if (partition->filesystem == NULL) { + printf("Null filesystem!\n"); + return rv; + } + if (!read_only) { + rv = mount(devname, mount_point, partition->filesystem, flags, NULL); + } + if (read_only || rv < 0) { + rv = mount(devname, mount_point, partition->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); + } + } + return rv; +} + +int +mmc_raw_copy (const MmcPartition *partition, char *in_file) { + int ch; + FILE *in; + FILE *out; + int val = 0; + char buf[512]; + unsigned sz = 0; + unsigned i; + int ret = -1; + char *out_file = partition->device_index; + + in = fopen ( in_file, "r" ); + if (in == NULL) + goto ERROR3; + + out = fopen ( out_file, "w" ); + if (out == NULL) + goto ERROR2; + + fseek(in, 0L, SEEK_END); + sz = ftell(in); + fseek(in, 0L, SEEK_SET); + + if (sz % 512) + { + while ( ( ch = fgetc ( in ) ) != EOF ) + fputc ( ch, out ); + } + else + { + for (i=0; i< (sz/512); i++) + { + if ((fread(buf, 512, 1, in)) != 1) + goto ERROR1; + if ((fwrite(buf, 512, 1, out)) != 1) + goto ERROR1; + } + } + + fsync(out); + ret = 0; +ERROR1: + fclose ( out ); +ERROR2: + fclose ( in ); +ERROR3: + return ret; + +} + diff --git a/mmcutils/mmcutils.h b/mmcutils/mmcutils.h new file mode 100644 index 0000000..bab494a --- /dev/null +++ b/mmcutils/mmcutils.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2010, Code Aurora Forum. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of Code Aurora Forum, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef MMCUTILS_H_ +#define MMCUTILS_H_ + +/* Some useful define used to access the MBR/EBR table */ +#define BLOCK_SIZE 0x200 +#define TABLE_ENTRY_0 0x1BE +#define TABLE_ENTRY_1 0x1CE +#define TABLE_ENTRY_2 0x1DE +#define TABLE_ENTRY_3 0x1EE +#define TABLE_SIGNATURE 0x1FE +#define TABLE_ENTRY_SIZE 0x010 + +#define OFFSET_STATUS 0x00 +#define OFFSET_TYPE 0x04 +#define OFFSET_FIRST_SEC 0x08 +#define OFFSET_SIZE 0x0C +#define COPYBUFF_SIZE (1024 * 16) +#define BINARY_IN_TABLE_SIZE (16 * 512) +#define MAX_FILE_ENTRIES 20 + +#define MMC_BOOT_TYPE 0x48 +#define MMC_SYSTEM_TYPE 0x82 +#define MMC_USERDATA_TYPE 0x83 + +#define MMC_RCA 2 + +#define MAX_PARTITIONS 64 + +#define GET_LWORD_FROM_BYTE(x) ((unsigned)*(x) | \ + ((unsigned)*((x)+1) << 8) | \ + ((unsigned)*((x)+2) << 16) | \ + ((unsigned)*((x)+3) << 24)) + +#define PUT_LWORD_TO_BYTE(x, y) do{*(x) = (y) & 0xff; \ + *((x)+1) = ((y) >> 8) & 0xff; \ + *((x)+2) = ((y) >> 16) & 0xff; \ + *((x)+3) = ((y) >> 24) & 0xff; }while(0) + +#define GET_PAR_NUM_FROM_POS(x) ((((x) & 0x0000FF00) >> 8) + ((x) & 0x000000FF)) + +#define MMC_BOOT_TYPE 0x48 +#define MMC_EXT3_TYPE 0x83 +#define MMC_VFAT_TYPE 0xC +typedef struct MmcPartition MmcPartition; + +/* Functions */ +int mmc_scan_partitions(); +const MmcPartition *mmc_find_partition_by_name(const char *name); +int mmc_format_ext3 (MmcPartition *partition); +int mmc_mount_partition(const MmcPartition *partition, const char *mount_point, \ + int read_only); +int mmc_raw_copy (const MmcPartition *partition, char *in_file); + +#endif // MMCUTILS_H_ + + -- cgit v1.1 From fef77c02533ee2e62c6827af5ba3cfcc2bd0e749 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Tue, 9 Nov 2010 20:03:42 -0800 Subject: Changes to support Vision recovery. Fixing up a lot of bugs related to the CodeAurora mmc commit. Change-Id: I9b71070fe41559a5d93d3c35efc3a511b7088e8e --- mmcutils/mmcutils.c | 74 +++++++++++++++++++++++++++++++++++++++++++---------- mmcutils/mmcutils.h | 12 ++++++++- 2 files changed, 71 insertions(+), 15 deletions(-) (limited to 'mmcutils') diff --git a/mmcutils/mmcutils.c b/mmcutils/mmcutils.c index d7e459a..f7df95d 100644 --- a/mmcutils/mmcutils.c +++ b/mmcutils/mmcutils.c @@ -47,16 +47,6 @@ char *ext3_partitions[] = {"system", "userdata", "cache", "NONE"}; unsigned vfat_count = 0; char *vfat_partitions[] = {"modem", "NONE"}; -struct MmcPartition { - char *device_index; - char *filesystem; - char *name; - unsigned dstatus; - unsigned dtype ; - unsigned dfirstsec; - unsigned dsize; -}; - typedef struct { MmcPartition *partitions; int partitions_allocd; @@ -80,6 +70,10 @@ mmc_partition_name (MmcPartition *mbr, unsigned int type) { sprintf(name,"boot"); mbr->name = strdup(name); break; + case MMC_RECOVERY_TYPE: + sprintf(name,"recovery"); + mbr->name = strdup(name); + break; case MMC_EXT3_TYPE: if (strcmp("NONE", ext3_partitions[ext3_count])) { strcpy((char *)name,(const char *)ext3_partitions[ext3_count]); @@ -291,9 +285,9 @@ mmc_find_partition_by_name(const char *name) return NULL; } -#define MKE2FS_BIN "/sbin/mke2fs_static" -#define TUNE2FS_BIN "/sbin/tune2fs_static" -#define E2FSCK_BIN "/sbin/e2fsck_static" +#define MKE2FS_BIN "/sbin/mke2fs" +#define TUNE2FS_BIN "/sbin/tune2fs" +#define E2FSCK_BIN "/sbin/e2fsck" static int run_exec_process ( char **argv) { @@ -323,7 +317,7 @@ mmc_format_ext3 (MmcPartition *partition) { return -1; // Run tune2fs - char *const tune2fs[] = {TUNE2FS_BIN, "-C", "1", device, NULL}; + char *const tune2fs[] = {TUNE2FS_BIN, "-j", "-C", "1", device, NULL}; if(run_exec_process(tune2fs)) return -1; @@ -413,3 +407,55 @@ ERROR3: } + +int +mmc_raw_dump (const MmcPartition *partition, char *out_file) { + int ch; + FILE *in; + FILE *out; + int val = 0; + char buf[512]; + unsigned sz = 0; + unsigned i; + int ret = -1; + char *in_file = partition->device_index; + + in = fopen ( in_file, "r" ); + if (in == NULL) + goto ERROR3; + + out = fopen ( out_file, "w" ); + if (out == NULL) + goto ERROR2; + + fseek(in, 0L, SEEK_END); + sz = ftell(in); + fseek(in, 0L, SEEK_SET); + + if (sz % 512) + { + while ( ( ch = fgetc ( in ) ) != EOF ) + fputc ( ch, out ); + } + else + { + for (i=0; i< (sz/512); i++) + { + if ((fread(buf, 512, 1, in)) != 1) + goto ERROR1; + if ((fwrite(buf, 512, 1, out)) != 1) + goto ERROR1; + } + } + + fsync(out); + ret = 0; +ERROR1: + fclose ( out ); +ERROR2: + fclose ( in ); +ERROR3: + return ret; + +} + diff --git a/mmcutils/mmcutils.h b/mmcutils/mmcutils.h index bab494a..6a3070d 100644 --- a/mmcutils/mmcutils.h +++ b/mmcutils/mmcutils.h @@ -50,6 +50,7 @@ #define MMC_BOOT_TYPE 0x48 #define MMC_SYSTEM_TYPE 0x82 #define MMC_USERDATA_TYPE 0x83 +#define MMC_RECOVERY_TYPE 0x71 #define MMC_RCA 2 @@ -70,7 +71,15 @@ #define MMC_BOOT_TYPE 0x48 #define MMC_EXT3_TYPE 0x83 #define MMC_VFAT_TYPE 0xC -typedef struct MmcPartition MmcPartition; +typedef struct MmcPartition { + char *device_index; + char *filesystem; + char *name; + unsigned dstatus; + unsigned dtype ; + unsigned dfirstsec; + unsigned dsize; +} MmcPartition; /* Functions */ int mmc_scan_partitions(); @@ -79,6 +88,7 @@ int mmc_format_ext3 (MmcPartition *partition); int mmc_mount_partition(const MmcPartition *partition, const char *mount_point, \ int read_only); int mmc_raw_copy (const MmcPartition *partition, char *in_file); +int mmc_raw_dump (const MmcPartition *partition, char *out_file); #endif // MMCUTILS_H_ -- cgit v1.1 From 19447c05506e4dfd19f621081770fe03e29d0457 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Wed, 10 Nov 2010 10:40:44 -0800 Subject: Refactor recovery's block device handling to work across variant hardware in a cleaner fashion. Re add firmware update Change-Id: I699ad22390ed14e597d17a7bcb32ad1b1af00b4b support mmc misc Change-Id: Iff02f8d03db6835f501d052140cebeefee521305 fix compile errors Change-Id: I032edbd157a8a15f561bb83330c715ebaa008d18 fix compile errors Change-Id: Idff3449be3376f22fceefc2c35637527f8df8f3f Initial work to clean up the block devices. Change-Id: I4be20ac124864a281be9cd116e211a2618404a27 all done Change-Id: I0338f62f6a045556ebe90b0200685be113178319 fix up nandroid Change-Id: I886f00271183e6d2921c080b0939341f2cf12a4d --- mmcutils/mmcutils.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mmcutils/mmcutils.h | 13 ++---- 2 files changed, 132 insertions(+), 10 deletions(-) (limited to 'mmcutils') diff --git a/mmcutils/mmcutils.c b/mmcutils/mmcutils.c index f7df95d..f0cdf8c 100644 --- a/mmcutils/mmcutils.c +++ b/mmcutils/mmcutils.c @@ -47,6 +47,16 @@ char *ext3_partitions[] = {"system", "userdata", "cache", "NONE"}; unsigned vfat_count = 0; char *vfat_partitions[] = {"modem", "NONE"}; +struct MmcPartition { + char *device_index; + char *filesystem; + char *name; + unsigned dstatus; + unsigned dtype ; + unsigned dfirstsec; + unsigned dsize; +}; + typedef struct { MmcPartition *partitions; int partitions_allocd; @@ -408,6 +418,7 @@ ERROR3: } +// TODO: refactor this to not be a giant copy paste mess int mmc_raw_dump (const MmcPartition *partition, char *out_file) { int ch; @@ -459,3 +470,121 @@ ERROR3: } + +int +mmc_raw_read (const MmcPartition *partition, char *data, int data_size) { + int ch; + FILE *in; + int val = 0; + char buf[512]; + unsigned sz = 0; + unsigned i; + int ret = -1; + char *in_file = partition->device_index; + + in = fopen ( in_file, "r" ); + if (in == NULL) + goto ERROR3; + + fseek(in, 0L, SEEK_END); + sz = ftell(in); + fseek(in, 0L, SEEK_SET); + + fread(data, data_size, 1, in); + + ret = 0; +ERROR1: +ERROR2: + fclose ( in ); +ERROR3: + return ret; + +} + +int +mmc_raw_write (const MmcPartition *partition, char *data, int data_size) { + int ch; + FILE *out; + int val = 0; + char buf[512]; + unsigned sz = 0; + unsigned i; + int ret = -1; + char *out_file = partition->device_index; + + out = fopen ( out_file, "w" ); + if (out == NULL) + goto ERROR3; + + fwrite(data, data_size, 1, out); + + ret = 0; +ERROR1: +ERROR2: + fclose ( out ); +ERROR3: + return ret; + +} + +int restore_raw_partition(const char *partition, const char *filename) +{ + mmc_scan_partitions(); + const MmcPartition *p; + p = mmc_find_partition_by_name(partition); + if (p == NULL) + return -1; + return mmc_raw_copy(p, filename); +} + +int backup_raw_partition(const char *partition, const char *filename) +{ + mmc_scan_partitions(); + const MmcPartition *p; + p = mmc_find_partition_by_name(partition); + if (p == NULL) + return -1; + return mmc_raw_dump(p, filename); +} + +int erase_raw_partition(const char *partition) +{ + mmc_scan_partitions(); + const MmcPartition *p; + p = mmc_find_partition_by_name(partition); + if (p == NULL) + return -1; + + // TODO: implement raw wipe + return 0; +} + +int erase_partition(const char *partition, const char *filesystem) +{ + mmc_scan_partitions(); + const MmcPartition *p; + p = mmc_find_partition_by_name(partition); + if (p == NULL) + return -1; + return mmc_format_ext3 (p); +} + +int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only) +{ + mmc_scan_partitions(); + const MmcPartition *p; + p = mmc_find_partition_by_name(partition); + if (p == NULL) + return -1; + return mmc_mount_partition(p, mount_point, read_only); +} + +const char* get_partition_device(const char *partition) +{ + mmc_scan_partitions(); + const MmcPartition *p; + p = mmc_find_partition_by_name(partition); + if (p == NULL) + return NULL; + return p->device_index; +} diff --git a/mmcutils/mmcutils.h b/mmcutils/mmcutils.h index 6a3070d..64e5813 100644 --- a/mmcutils/mmcutils.h +++ b/mmcutils/mmcutils.h @@ -71,15 +71,7 @@ #define MMC_BOOT_TYPE 0x48 #define MMC_EXT3_TYPE 0x83 #define MMC_VFAT_TYPE 0xC -typedef struct MmcPartition { - char *device_index; - char *filesystem; - char *name; - unsigned dstatus; - unsigned dtype ; - unsigned dfirstsec; - unsigned dsize; -} MmcPartition; +typedef struct MmcPartition MmcPartition; /* Functions */ int mmc_scan_partitions(); @@ -88,7 +80,8 @@ int mmc_format_ext3 (MmcPartition *partition); int mmc_mount_partition(const MmcPartition *partition, const char *mount_point, \ int read_only); int mmc_raw_copy (const MmcPartition *partition, char *in_file); -int mmc_raw_dump (const MmcPartition *partition, char *out_file); +int mmc_raw_read (const MmcPartition *partition, char *data, int data_size); +int mmc_raw_write (const MmcPartition *partition, char *data, int data_size); #endif // MMCUTILS_H_ -- cgit v1.1 From 12154200a76e70f5da0f135abf5ffe060f35a785 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Thu, 11 Nov 2010 01:16:14 -0800 Subject: fix up implementation of int get_partition_device Change-Id: I142b15228322790892dd07b10d6a3f31440badc7 --- mmcutils/mmcutils.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'mmcutils') diff --git a/mmcutils/mmcutils.c b/mmcutils/mmcutils.c index f0cdf8c..2c10343 100644 --- a/mmcutils/mmcutils.c +++ b/mmcutils/mmcutils.c @@ -579,12 +579,13 @@ int mount_partition(const char *partition, const char *mount_point, const char * return mmc_mount_partition(p, mount_point, read_only); } -const char* get_partition_device(const char *partition) +int get_partition_device(const char *partition, char *device) { mmc_scan_partitions(); const MmcPartition *p; p = mmc_find_partition_by_name(partition); if (p == NULL) - return NULL; - return p->device_index; + return -1; + strcpy(device, p->device_index); + return 0; } -- cgit v1.1 From 4123b582992c9b50d6ff11cc08811970a472a937 Mon Sep 17 00:00:00 2001 From: Steve Kondik Date: Sun, 14 Nov 2010 03:18:40 -0500 Subject: recovery: Autodetection of device flash type Detect flash type at runtime rather than requiring this to be set in the device configuration. The detection is based on the existence of /proc/mtd, /proc/emmc, or /dev/block/bml1. Change-Id: I464962a567022c5862c249f06d36c2d1cddeacba --- mmcutils/mmcutils.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'mmcutils') diff --git a/mmcutils/mmcutils.c b/mmcutils/mmcutils.c index 2c10343..44fdcf2 100644 --- a/mmcutils/mmcutils.c +++ b/mmcutils/mmcutils.c @@ -527,7 +527,7 @@ ERROR3: } -int restore_raw_partition(const char *partition, const char *filename) +int cmd_mmc_restore_raw_partition(const char *partition, const char *filename) { mmc_scan_partitions(); const MmcPartition *p; @@ -537,7 +537,7 @@ int restore_raw_partition(const char *partition, const char *filename) return mmc_raw_copy(p, filename); } -int backup_raw_partition(const char *partition, const char *filename) +int cmd_mmc_backup_raw_partition(const char *partition, const char *filename) { mmc_scan_partitions(); const MmcPartition *p; @@ -547,19 +547,19 @@ int backup_raw_partition(const char *partition, const char *filename) return mmc_raw_dump(p, filename); } -int erase_raw_partition(const char *partition) +int cmd_mmc_erase_raw_partition(const char *partition) { mmc_scan_partitions(); const MmcPartition *p; p = mmc_find_partition_by_name(partition); if (p == NULL) return -1; - + // TODO: implement raw wipe return 0; } -int erase_partition(const char *partition, const char *filesystem) +int cmd_mmc_erase_partition(const char *partition, const char *filesystem) { mmc_scan_partitions(); const MmcPartition *p; @@ -569,7 +569,7 @@ int erase_partition(const char *partition, const char *filesystem) return mmc_format_ext3 (p); } -int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only) +int cmd_mmc_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only) { mmc_scan_partitions(); const MmcPartition *p; @@ -579,7 +579,7 @@ int mount_partition(const char *partition, const char *mount_point, const char * return mmc_mount_partition(p, mount_point, read_only); } -int get_partition_device(const char *partition, char *device) +int cmd_mmc_get_partition_device(const char *partition, char *device) { mmc_scan_partitions(); const MmcPartition *p; -- cgit v1.1