Skip to content

Commit

Permalink
Added support for boot and recovery partitions backup
Browse files Browse the repository at this point in the history
EMMC of MTK6589 is a large continuous block covering
all partitions. Recovery and boot partitions definitions are
somewhat hidden from MBR and /proc/emmc disabling CWM
to determine correct sizes of these partitions automatically.
This results in CWM using standard fseek/ftell method which
returns the size of the whole EMMC which can be several Gigabytes.

This commit enables to define sizes for boot and recovery partitions
explicitly using BoardConfig flags - so the correct number of bytes
can be copied from these devices during backup.

Two pairs of config flags are available:
CWM_EMMC_BOOT_DEVICE_NAME: defines the name of boot partition device
(typically /dev/bootimg)
CWM_EMMC_BOOT_DEVICE_SIZE: defines size of boot partition in bytes

CWM_EMMC_RECOVERY_DEVICE_NAME: defines the name of recovery partition device
(typically /dev/recovery)
CWM_EMMC_RECOVERY_DEVICE_SIZE: defines size of recovery partition in bytes

During backup, the name of partition being processed is checked against
defined *DEVICE_NAME flag. If matches then size is set from the corresponding
*DEVICE_SIZE flag.

Change-Id: Ia5b8fbd50ff234301ce812e39a648c5cce87e284
  • Loading branch information
C3C0 committed Apr 27, 2013
1 parent 4d9a03b commit 226e86a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
16 changes: 16 additions & 0 deletions mmcutils/Android.mk
Expand Up @@ -3,6 +3,22 @@ ifneq ($(TARGET_SIMULATOR),true)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

ifneq ($(CWM_EMMC_BOOT_DEVICE_NAME),)
LOCAL_CFLAGS += -DCWM_EMMC_BOOT_DEVICE_NAME="$(CWM_EMMC_BOOT_DEVICE_NAME)"
endif

ifneq ($(CWM_EMMC_BOOT_DEVICE_SIZE),)
LOCAL_CFLAGS += -DCWM_EMMC_BOOT_DEVICE_SIZE=$(CWM_EMMC_BOOT_DEVICE_SIZE)
endif

ifneq ($(CWM_EMMC_RECOVERY_DEVICE_NAME),)
LOCAL_CFLAGS += -DCWM_EMMC_RECOVERY_DEVICE_NAME="$(CWM_EMMC_RECOVERY_DEVICE_NAME)"
endif

ifneq ($(CWM_EMMC_RECOVERY_DEVICE_SIZE),)
LOCAL_CFLAGS += -DCWM_EMMC_RECOVERY_DEVICE_SIZE=$(CWM_EMMC_RECOVERY_DEVICE_SIZE)
endif

LOCAL_SRC_FILES := \
mmcutils.c

Expand Down
47 changes: 37 additions & 10 deletions mmcutils/mmcutils.c
Expand Up @@ -41,6 +41,9 @@

#include "mmcutils.h"

#define XSTR(x) #x
#define STR(x) XSTR(x)

unsigned ext3_count = 0;
char *ext3_partitions[] = {"system", "userdata", "cache", "NONE"};

Expand Down Expand Up @@ -471,13 +474,12 @@ mmc_raw_copy (const MmcPartition *partition, char *in_file) {


int
mmc_raw_dump_internal (const char* in_file, const char *out_file) {
mmc_raw_dump_internal (const char* in_file, const char *out_file, unsigned size) {
int ch;
FILE *in;
FILE *out;
int val = 0;
char buf[512];
unsigned sz = 0;
unsigned i;
int ret = -1;

Expand All @@ -489,18 +491,26 @@ mmc_raw_dump_internal (const char* in_file, const char *out_file) {
if (out == NULL)
goto ERROR2;

fseek(in, 0L, SEEK_END);
sz = ftell(in);
fseek(in, 0L, SEEK_SET);
if (size == 0)
{
fseek(in, 0L, SEEK_END);
size = ftell(in);
fseek(in, 0L, SEEK_SET);
}

if (sz % 512)
if (size % 512)
{
unsigned counter = 0;
while ( ( ch = fgetc ( in ) ) != EOF )
{
fputc ( ch, out );
if (++counter == size)
break;
}
}
else
{
for (i=0; i< (sz/512); i++)
for (i=0; i< (size/512); i++)
{
if ((fread(buf, 512, 1, in)) != 1)
goto ERROR1;
Expand All @@ -523,7 +533,7 @@ mmc_raw_dump_internal (const char* in_file, const char *out_file) {
// TODO: refactor this to not be a giant copy paste mess
int
mmc_raw_dump (const MmcPartition *partition, char *out_file) {
return mmc_raw_dump_internal(partition->device_index, out_file);
return mmc_raw_dump_internal(partition->device_index, out_file, 0);
}


Expand Down Expand Up @@ -594,7 +604,7 @@ int cmd_mmc_restore_raw_partition(const char *partition, const char *filename)
return mmc_raw_copy(p, filename);
}
else {
return mmc_raw_dump_internal(filename, partition);
return mmc_raw_dump_internal(filename, partition, 0);
}
}

Expand All @@ -609,7 +619,24 @@ int cmd_mmc_backup_raw_partition(const char *partition, const char *filename)
return mmc_raw_dump(p, filename);
}
else {
return mmc_raw_dump_internal(partition, filename);
unsigned size = 0;

// take boot and recovery partition sizes into account if defined
#if defined(CWM_EMMC_BOOT_DEVICE_NAME) && defined(CWM_EMMC_BOOT_DEVICE_SIZE)
if (strcmp(partition, STR(CWM_EMMC_BOOT_DEVICE_NAME)) == 0) {
size = CWM_EMMC_BOOT_DEVICE_SIZE;
printf("CWM_EMMC_BOOT_DEVICE: %s; Size: 0x%x\n", partition, size);
}
#endif

#if defined(CWM_EMMC_RECOVERY_DEVICE_NAME) && defined(CWM_EMMC_RECOVERY_DEVICE_SIZE)
if (strcmp(partition, STR(CWM_EMMC_RECOVERY_DEVICE_NAME)) == 0) {
size = CWM_EMMC_RECOVERY_DEVICE_SIZE;
printf("CWM_EMMC_RECOVERY_DEVICE: %s; Size: 0x%x\n", partition, size);
}
#endif

return mmc_raw_dump_internal(partition, filename, size);
}
}

Expand Down

7 comments on commit 226e86a

@chongbo2013
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for you , my device is mt6589 , but i can't mount the usb SD card on the cwm recovery6031

@C3C0
Copy link
Owner Author

@C3C0 C3C0 commented on 226e86a Apr 30, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mounting works fine here. Maybe your local driver issue? Note, that only external SD can be mounted as USB storage.

@chongbo2013
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USE_CAMERA_STUB := true

inherit from the proprietary version

-include vendor/mogu/m2/BoardConfigVendor.mk

TARGET_ARCH= arm
TARGET_NO_BOOTLOADER := true
TARGET_BOARD_PLATFORM := mt657x
TARGET_ARCH_VARIANT_CPU := cortex-a9
TARGET_CPU_ABI := armeabi
TARGET_ARCH_VARIANT := armv7-a-neon
ARCH_ARM_HAVE_TLS_REGISTER := true

TARGET_PREBUILT_KERNEL := device/mogu/m2/kernel
BOARD_KERNEL_CMDLINE :=
BOARD_KERNEL_BASE := 0x10000000
BOARD_KERNEL_PAGESIZE := 2048

TARGET_BOOTLOADER_BOARD_NAME := m2

BOARD_HAS_NO_SELECT_BUTTON := true

Recovery

TARGET_RECOVERY_FSTAB := device/mogu/m2/recovery/recovery.fstab
TARGET_RECOVERY_INITRC := device/mogu/m2/recovery/recovery.rc
BOARD_CUSTOM_RECOVERY_KEYMAPPING := ../../device/mogu/m2/recovery/recovery_keys.c
CWM_EMMC_BOOT_DEVICE_NAME := /dev/bootimg
CWM_EMMC_BOOT_DEVICE_SIZE := 0x105c0000
CWM_EMMC_RECOVERY_DEVICE_NAME := /dev/recovery
CWM_EMMC_RECOVERY_DEVICE_SIZE := 0x105c0000
BOARD_BOOTIMAGE_PARTITION_SIZE := 0x105c0000
BOARD_RECOVERYIMAGE_PARTITION_SIZE := 0x105c0000
BOARD_SYSTEMIMAGE_PARTITION_SIZE := 0x105c0000
BOARD_USERDATAIMAGE_PARTITION_SIZE := 0x105c0000
BOARD_FLASH_BLOCK_SIZE := 131072
BOARD_UMS_LUNFILE := /sys/devices/platform/mt_usb/gadget/lun%d/file

@chongbo2013
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is my BoardConfig.mk .Maybe your local driver issue? how to know driver issue ,usb connection can't mount the external storage

@C3C0
Copy link
Owner Author

@C3C0 C3C0 commented on 226e86a Apr 30, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use my device config from here:
https://github.com/C3C0/android_device_thl_w8 (compatible with android 4.1.2)
and
https://github.com/C3C0/android_device_thl_w8/tree/cwm-4.2 (compatible with android 4.2.1)

You could also wait for the newer version (6.0.3.2) which will include further improvements.

@chongbo2013
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a line:
BOARD_UMS_LUNFILE := /sys/devices/platform/mt_usb/gadget/lun%d/file
init.rc add:
write /sys/device/platform/mt_usb/cmode 1

but usb cannot be mounted too.

@chongbo2013
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you very much!!

Please sign in to comment.