Update twrp.sh OTA with xz and sparse image support

This commit is contained in:
Pierre-Hugues Husson 2018-12-09 23:24:26 +01:00
parent 14d7c12a97
commit 25e6438ba5
4 changed files with 132 additions and 10 deletions

View File

@ -72,6 +72,9 @@ PRODUCT_COPY_FILES += \
device/phh/treble/twrp/twrp.sh:system/bin/twrp.sh \
device/phh/treble/twrp/busybox-armv7l:system/bin/busybox_phh
PRODUCT_PACKAGES += \
simg2img_simple
ifneq (,$(wildcard external/exfat))
PRODUCT_PACKAGES += \
mkfs.exfat \

View File

@ -16,3 +16,11 @@ cc_binary {
"uevent.cpp",
],
}
cc_binary {
name: "simg2img_simple",
srcs: [
"simg2img_simple.cpp",
],
host_supported: true,
}

97
cmds/simg2img_simple.cpp Normal file
View File

@ -0,0 +1,97 @@
#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include <sys/sendfile.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct sparse_header {
uint32_t magic; /* 0xed26ff3a */
uint16_t major_version; /* (0x1) - reject images with higher major versions */
uint16_t minor_version; /* (0x0) - allow images with higer minor versions */
uint16_t file_hdr_sz; /* 28 bytes for first revision of the file format */
uint16_t chunk_hdr_sz; /* 12 bytes for first revision of the file format */
uint32_t blk_sz; /* block size in bytes, must be a multiple of 4 (4096) */
uint32_t total_blks; /* total blocks in the non-sparse output image */
uint32_t total_chunks; /* total chunks in the sparse input image */
uint32_t image_checksum; /* CRC32 checksum of the original data, counting "don't care" */
/* as 0. Standard 802.3 polynomial, use a Public Domain */
/* table implementation */
} sparse_header_t;
#define SPARSE_HEADER_MAGIC 0xed26ff3a
#define CHUNK_TYPE_RAW 0xCAC1
#define CHUNK_TYPE_FILL 0xCAC2
#define CHUNK_TYPE_DONT_CARE 0xCAC3
#define CHUNK_TYPE_CRC32 0xCAC4
typedef struct chunk_header {
uint16_t chunk_type; /* 0xCAC1 -> raw; 0xCAC2 -> fill; 0xCAC3 -> don't care */
uint16_t reserved1;
uint32_t chunk_sz; /* in blocks in output image */
uint32_t total_sz; /* in bytes of chunk input file including chunk header and data */
} chunk_header_t;
/* Following a Raw or Fill or CRC32 chunk is data.
* For a Raw chunk, it's the data in chunk_sz * blk_sz.
* For a Fill chunk, it's 4 bytes of the fill data.
* For a CRC32 chunk, it's 4 bytes of CRC32
*/
void nsendfile(int out_fd, int in_fd, size_t count) {
while(count) {
ssize_t res = splice(in_fd, NULL, out_fd, NULL, count, 0);
if(res == 0 || res == -1) exit(112);
count -= res;
}
}
int main() {
sparse_header_t hdr;
if(read(0, &hdr, sizeof(hdr)) != sizeof(hdr)) exit(1);
if(hdr.magic != SPARSE_HEADER_MAGIC) exit(2);
if(hdr.blk_sz != 4096) exit(6);
if(hdr.major_version != 1) exit(11);
if(hdr.minor_version != 0) exit(12);
if(hdr.file_hdr_sz != 28) exit(13);
if(hdr.chunk_hdr_sz != 12) exit(14);
char block[4096];
for(unsigned i=0; i<hdr.total_chunks; i++) {
chunk_header_t chunk;
if(read(0, &chunk, sizeof(chunk)) != sizeof(chunk)) exit(3);
if(chunk.chunk_type == CHUNK_TYPE_RAW) {
if(chunk.total_sz != sizeof(chunk_header_t) + (chunk.chunk_sz * hdr.blk_sz)) exit(7);
nsendfile(1, 0, hdr.blk_sz * chunk.chunk_sz);
} else if(chunk.chunk_type == CHUNK_TYPE_FILL) {
if(chunk.total_sz != 4 + sizeof(chunk_header_t)) exit(7);
uint32_t fill;
if(read(0, &fill, sizeof(fill)) != sizeof(fill)) exit(5);
//memset takes a char, not a int32, hence the check
if(fill != 0) exit(6);
memset(block, fill, hdr.blk_sz);
for(unsigned i=0; i<chunk.chunk_sz; i++)
if(write(1, block, hdr.blk_sz) != hdr.blk_sz) exit(8);
} else if(chunk.chunk_type == CHUNK_TYPE_DONT_CARE) {
if(chunk.total_sz != sizeof(chunk_header_t)) exit(9);
memset(block, 0, hdr.blk_sz);
for(unsigned i=0; i<chunk.chunk_sz; i++)
if(write(1, block, hdr.blk_sz) != hdr.blk_sz) exit(10);
} else if(chunk.chunk_type == CHUNK_TYPE_CRC32) {
if(chunk.total_sz != 4 + sizeof(chunk_header_t)) exit(7);
uint32_t crc32;
if(read(0, &crc32, sizeof(crc32)) != sizeof(crc32)) exit(5);
/* ignore crc32 */
} else {
exit(4);
}
}
fsync(1);
return 0;
}

View File

@ -126,17 +126,31 @@ done
blockdev --setrw "$system"
size="$(sed -En '2s/^([0-9]+) .*/\1/p' /cache/phh/block.map)"
block_size="$(sed -En '2s/.* ([0-9]*)$/\1/p' /cache/phh/block.map)"
n_ranges="$(sed -n 3p /cache/phh/block.map)"
block_id=0
for i in $(seq 1 $n_ranges);do
range_start="$(sed -En $((i+3))'s/^([0-9]+) .*/\1/p' /cache/phh/block.map)"
range_end="$(sed -En $((i+3))'s/^.* ([0-9]+)$/\1/p' /cache/phh/block.map)"
n_blocks=$((range_end-range_start))
busybox_phh dd bs=$block_size skip=$range_start seek=$block_id count=$n_blocks if=$dev of=$system
block_id=$((block_id+n_blocks))
for method in xz-sparse sparse raw;do
(
size="$(sed -En '2s/^([0-9]+) .*/\1/p' /cache/phh/block.map)"
block_size="$(sed -En '2s/.* ([0-9]*)$/\1/p' /cache/phh/block.map)"
n_ranges="$(sed -n 3p /cache/phh/block.map)"
block_id=0
for i in $(seq 1 $n_ranges);do
range_start="$(sed -En $((i+3))'s/^([0-9]+) .*/\1/p' /cache/phh/block.map)"
range_end="$(sed -En $((i+3))'s/^.* ([0-9]+)$/\1/p' /cache/phh/block.map)"
n_blocks=$((range_end-range_start))
busybox_phh dd bs=$block_size skip=$range_start count=$n_blocks if=$dev
block_id=$((block_id+n_blocks))
done
) | (
set -e
if [ "$method" == xz-sparse ];then
busybox_phh xz -d -c | simg2img_simple > $system
elif [ "$method" == sparse ];then
simg2img_simple > $system
elif [ "$method" == raw ];then
cat > $system
fi
) && break
done
sync