M7350v1_en_gpl

This commit is contained in:
T
2024-09-09 08:52:07 +00:00
commit f9cc65cfda
65988 changed files with 26357421 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
/* Copyright (c) 2009-2011, The Linux Foundation. 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 The Linux Foundation 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 BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 <reg.h>
#include <debug.h>
#include <smem.h>
#include <stdint.h>
#include <lib/ptable.h>
#define SIZE_1M (1024 * 1024)
#define SIZE_8M (8 * SIZE_1M)
#define SIZE_15M (15 * SIZE_1M)
#define SIZE_17M (17 * SIZE_1M)
#define SIZE_23M (23 * SIZE_1M)
#define SIZE_88M (11 * SIZE_8M)
unsigned *target_atag_mem(unsigned *ptr)
{
struct smem_ram_ptable ram_ptable;
uint8_t i = 0;
struct ptable *nand_ptable;
struct ptentry *dsp3_ptn;
unsigned int size;
if (smem_ram_ptable_init(&ram_ptable)) {
for (i = 0; i < ram_ptable.len; i++) {
if (ram_ptable.parts[i].category == SDRAM &&
ram_ptable.parts[i].type == SYS_MEMORY &&
ram_ptable.parts[i].start == 0x40000000) {
ASSERT(ram_ptable.parts[i].size >= SIZE_15M);
*ptr++ = 4;
*ptr++ = 0x54410002;
*ptr++ = SIZE_15M;
*ptr++ = ram_ptable.parts[i].start + SIZE_8M;
*ptr++ = 4;
*ptr++ = 0x54410002;
*ptr++ = SIZE_23M;
*ptr++ = ram_ptable.parts[i].start + SIZE_88M;
nand_ptable = flash_get_ptable();
dsp3_ptn = ptable_find(nand_ptable, "dsp3");
/* Check for DSP3 partition and its size */
if(dsp3_ptn != NULL){
size = dsp3_ptn->start;
size += dsp3_ptn->length;
if(size != dsp3_ptn->start)
continue;
}
/*Add additional atag to pass DSP3 memory to kernel*/
*ptr++ = 4;
*ptr++ = 0x54410002;
*ptr++ = SIZE_17M;
*ptr++ = ram_ptable.parts[i].start + SIZE_88M + SIZE_23M;
}
}
} else {
dprintf(CRITICAL, "ERROR: Unable to read RAM partition\n");
ASSERT(0);
}
return ptr;
}
void *target_get_scratch_address(void)
{
return ((void *)SCRATCH_ADDR);
}
unsigned target_get_max_flash_size(void)
{
return (SIZE_23M);
}

View File

@@ -0,0 +1,387 @@
/*
* Copyright (c) 2009, Google Inc.
* All rights reserved.
*
* Copyright (c) 2009-2013, The Linux Foundation. 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 Google, 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 BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE 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 <reg.h>
#include <debug.h>
#include <lib/ptable.h>
#include <dev/flash.h>
#include <dev/pm8921.h>
#include <dev/ssbi.h>
#include <string.h>
#include <smem.h>
#include <gsbi.h>
#include <platform/iomap.h>
#include <platform.h>
#define MDM9X15_CDP 3675
#define MDM9X15_MTP 3681
#define LINUX_MACHTYPE MDM9X15_CDP
#define RECOVERY_MODE 0x77665502
#define FOTA_COOKIE 0x64645343
#define FOTA_PARTITION 5
#define DSP3_PARTITION 5
static struct ptable flash_ptable;
unsigned hw_platform = 0;
unsigned target_msm_id = 0;
/* Partition names for fastboot flash */
static char *apps_ptn_names[] = { "aboot", "boot", "cache", "misc", "recovery", "fota",
"recoveryfs", "system", "userdata" };
/* Partitions should be in this order */
static char *ptable_ptn_names[] = { "APPSBL", "APPS", "CACHE", "MISC", "RECOVERY",
"FOTA", "RECOVERYFS", "SYSTEM", "USERDATA" };
static unsigned ptn_name_count = 9;
/* Apps partition flags to detect the presence of FOTA partitions.
* Initially, assume that the FOTA partitions are absent.
*/
static unsigned int apps_ptn_flag[] = {1, 1, 0, 0, 0, 0, 0, 1, 1};
/* Modem partition flags to detect the presence of DSP3 partitions.
* Initially, assume that the DSP3 partition is absent.
* The order of modem partiotions -
* "mibib", "sbl2", "rpm", "efs2", "dsp1", "dsp3", "dsp2"
*/
static unsigned int modem_ptn_flag[] = {1, 1, 1, 1, 1, 0, 1};
static unsigned modem_ptn_count = 7;
static const uint8_t uart_gsbi_id = GSBI_ID_4;
static pm8921_dev_t pmic;
void smem_ptable_init(void);
extern void reboot(unsigned reboot_reason);
void update_ptable_apps_partitions(void);
void update_ptable_modem_partitions(void);
void update_ptable_reorder(void);
extern int fake_key_get_state(void);
void target_init(void)
{
struct flash_info *flash_info;
dprintf(INFO, "target_init()\n");
/* Initialize PMIC driver */
pmic.read = (pm8921_read_func) & pa1_ssbi2_read_bytes;
pmic.write = (pm8921_write_func) & pa1_ssbi2_write_bytes;
pm8921_init(&pmic);
ptable_init(&flash_ptable);
smem_ptable_init();
flash_init();
flash_info = flash_get_info();
ASSERT(flash_info);
smem_add_modem_partitions(&flash_ptable);
/* Update the naming for apps partitions and type */
update_ptable_apps_partitions();
/* Update modem partitions to lower case for fastboot */
update_ptable_modem_partitions();
ptable_dump(&flash_ptable);
/* Reorder the partition table */
update_ptable_reorder();
flash_set_ptable( &flash_ptable);
}
static int read_from_flash(struct ptentry* ptn, int offset, int size, void *dest)
{
void *buffer = NULL;
unsigned page_size = flash_page_size();
unsigned page_mask = page_size - 1;
int read_size = (size + page_mask) & (~page_mask);
buffer = malloc(read_size);
if(!buffer){
dprintf(CRITICAL, "ERROR : Malloc failed for read_from_flash \n");
return -1;
}
if(flash_read(ptn, offset, buffer, read_size)){
dprintf(CRITICAL, "ERROR : Flash read failed \n");
return -1;
}
memcpy(dest, buffer, size);
free(buffer);
return 0;
}
static unsigned int get_fota_cookie(void)
{
struct ptentry *ptn;
struct ptable *ptable;
unsigned int cookie = 0;
ptable = flash_get_ptable();
if (ptable == NULL) {
dprintf(CRITICAL, "ERROR: Partition table not found\n");
return 0;
}
ptn = ptable_find(ptable, "fota");
if (ptn == NULL) {
dprintf(CRITICAL, "ERROR: No fota partition found\n");
return 0;
}
if(read_from_flash(ptn, 0, sizeof(unsigned int), &cookie) == -1){
dprintf(CRITICAL,"ERROR: failed to read fota cookie from flash");
return 0;
}
return cookie;
}
unsigned check_reboot_mode(void)
{
unsigned restart_reason = 0;
unsigned int cookie = 0;
/* Read reboot reason and scrub it */
restart_reason = readl(RESTART_REASON_ADDR);
writel(0x00, RESTART_REASON_ADDR);
/* SMEM value is not relied upon on power shutdown.
* Check either of SMEM or FOTA update cookie is set.
* Check fota cookie only when fota partition is available on target
*/
if (apps_ptn_flag[FOTA_PARTITION] == 1) {
cookie = get_fota_cookie();
if (cookie == FOTA_COOKIE)
return RECOVERY_MODE;
}
return restart_reason;
}
void board_info(void)
{
struct smem_board_info_v4 board_info_v4;
struct smem_board_info_v7 board_info_v7;
unsigned int board_info_len = 0;
unsigned smem_status;
unsigned format = 0;
unsigned id = 0;
if (hw_platform && target_msm_id)
return;
hw_platform = MDM9X15_CDP;
target_msm_id = MDM9600;
smem_status = smem_read_alloc_entry_offset(SMEM_BOARD_INFO_LOCATION,
&format, sizeof(format), 0);
if (!smem_status) {
if (format == 4) {
board_info_len = sizeof(board_info_v4);
smem_status =
smem_read_alloc_entry(SMEM_BOARD_INFO_LOCATION,
&board_info_v4,
board_info_len);
if (!smem_status) {
id = board_info_v4.board_info_v3.hw_platform;
target_msm_id =
board_info_v4.board_info_v3.msm_id;
}
} else if (format == 7) {
board_info_len = sizeof(board_info_v7);
smem_status =
smem_read_alloc_entry(SMEM_BOARD_INFO_LOCATION,
&board_info_v7,
board_info_len);
if (!smem_status) {
id = board_info_v7.board_info_v3.hw_platform;
target_msm_id =
board_info_v7.board_info_v3.msm_id;
}
} else
ASSERT(0);
}
return;
}
unsigned board_machtype(void)
{
board_info();
return hw_platform;
}
void reboot_device(unsigned reboot_reason)
{
/* Write reboot reason */
writel(reboot_reason, RESTART_REASON_ADDR);
/* Actually reset the chip */
pm8921_config_reset_pwr_off(1);
writel(0, MSM_PSHOLD_CTL_SU);
mdelay(10000);
dprintf(CRITICAL, "Rebooting failed\n");
return;
}
uint8_t target_uart_gsbi(void)
{
return uart_gsbi_id;
}
/*
* Return 1 to trigger to fastboot
*/
int fastboot_trigger(void)
{
int ret;
ret = fake_key_get_state();
/* Want to trigger when dip switch is on */
return (ret);
}
void update_ptable_modem_partitions(void)
{
uint32_t ptn_index, i = 0;
uint32_t name_size;
struct ptentry *ptentry_ptr = flash_ptable.parts;
struct ptentry *dsp3_ptn;
dsp3_ptn = ptable_find(&flash_ptable, "DSP3");
/* Check for DSP3 partition and its size */
if (dsp3_ptn != NULL)
if (dsp3_ptn->length > 0)
modem_ptn_flag[DSP3_PARTITION] = 1;
for (ptn_index = 0; ptn_index < modem_ptn_count; ptn_index++)
{
if (!modem_ptn_flag[ptn_index])
continue;
name_size = strlen(ptentry_ptr[ptn_index].name);
for (i = 0; i < name_size; i++)
{
ptentry_ptr[ptn_index].name[i] =
tolower(ptentry_ptr[ptn_index].name[i]);
}
ptentry_ptr[ptn_index].type = TYPE_APPS_PARTITION;
}
}
void update_ptable_apps_partitions(void)
{
uint32_t ptn_index, name_index = 0;
uint32_t end = 0xffffffff;
uint32_t name_size = strlen(ptable_ptn_names[name_index]);
struct ptentry *ptentry_ptr = flash_ptable.parts;
struct ptentry *fota_ptn;
unsigned int size;
fota_ptn = ptable_find(&flash_ptable, "FOTA");
/* Check for FOTA partitions and their size */
if (fota_ptn != NULL) {
if (fota_ptn->length > 0) {
/* FOTA partitions are present */
apps_ptn_flag[2] = 1;
apps_ptn_flag[3] = 1;
apps_ptn_flag[4] = 1;
apps_ptn_flag[5] = 1;
apps_ptn_flag[6] = 1;
}
}
for (ptn_index = 0; ptentry_ptr[ptn_index].start != end; ptn_index++) {
if (!(strncmp(ptentry_ptr[ptn_index].name,
ptable_ptn_names[name_index], name_size))) {
name_size = strlen(apps_ptn_names[name_index]);
name_size++; /* For null termination */
/* Update the partition names to something familiar */
if (name_size <= MAX_PTENTRY_NAME)
strncpy(ptentry_ptr[ptn_index].name,
apps_ptn_names[name_index], name_size);
/* Aboot uses modem page layout, leave aboot ptn */
if (name_index != 0)
ptentry_ptr[ptn_index].type =
TYPE_APPS_PARTITION;
/* Check for valid partitions
* according to the apps_ptn_flag
*/
do {
/* Don't go out of bounds */
name_index++;
if (name_index >= ptn_name_count)
goto ptn_name_update_done;
name_size =
strlen(ptable_ptn_names[name_index]);
} while (!apps_ptn_flag[name_index]);
}
}
ptn_name_update_done:
/* Update the end to be actual end for grow partition */
ptn_index--;
for (; ptentry_ptr[ptn_index].length != end; ptn_index++) {
};
/* If SMEM ptable is updated already then don't manually update this */
if (ptentry_ptr[ptn_index].start != end)
ptentry_ptr[ptn_index].length =
((struct flash_info *)flash_get_info())->num_blocks -
ptentry_ptr[ptn_index].start;
}
void update_ptable_reorder(void)
{
int boot_index;
struct ptentry boot_ptn;
boot_index = ptable_get_index(&flash_ptable, "boot");
if(boot_index == -1) {
dprintf (CRITICAL, "ERROR: Boot Partition not found. \n");
return;
}
boot_ptn = flash_ptable.parts[boot_index] ;
flash_ptable.parts[boot_index] = flash_ptable.parts[0];
flash_ptable.parts[0] = boot_ptn;
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright (c) 2011, The Linux Foundation. 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 The Linux Foundation 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 BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 <reg.h>
#include <platform/gpio.h>
#include <platform/iomap.h>
/* GPIO that controls the Dip Switch
* for FASTBOOT.
*/
#define DIP_SWITCH_GPIO 49
/*
* Fake keypad for 9x15
* Returns 0 if dip switch is off, 1 if on
*/
int fake_key_get_state(void)
{
int ret;
/* GPIO 49 connects to Boot Config5 */
gpio_tlmm_config(DIP_SWITCH_GPIO, 0, GPIO_OUTPUT, GPIO_PULL_DOWN,
GPIO_2MA, GPIO_ENABLE);
ret = readl(GPIO_IN_OUT_ADDR(DIP_SWITCH_GPIO));
return ret;
}

View File

@@ -0,0 +1,33 @@
LOCAL_DIR := $(GET_LOCAL_DIR)
INCLUDES += -I$(LOCAL_DIR)/include -I$(LK_TOP_DIR)/platform/msm_shared
PLATFORM := mdm9x15
MEMBASE := 0x41700000
MEMSIZE := 0x00100000 # 1MB
BASE_ADDR := 0x40800000
SCRATCH_ADDR := BASE_ADDR+0x05000000
DEFINES += NO_KEYPAD_DRIVER=1
MODULES += \
dev/keys \
dev/ssbi \
dev/pmic/pm8921 \
lib/ptable
DEFINES += \
SDRAM_SIZE=$(MEMSIZE) \
MEMBASE=$(MEMBASE) \
BASE_ADDR=$(BASE_ADDR) \
TAGS_ADDR=$(TAGS_ADDR) \
KERNEL_ADDR=$(KERNEL_ADDR) \
RAMDISK_ADDR=$(RAMDISK_ADDR) \
SCRATCH_ADDR=$(SCRATCH_ADDR)
OBJS += \
$(LOCAL_DIR)/init.o \
$(LOCAL_DIR)/atags.o \
$(LOCAL_DIR)/keypad.o

View File

@@ -0,0 +1,43 @@
#Makefile to generate appsboot.mbn
ifeq ($(BOOTLOADER_OUT),.)
APPSBOOTHEADER_DIR := $(BUILDDIR)
else
APPSBOOTHEADER_DIR := $(BOOTLOADER_OUT)/../..
endif
SRC_DIR := target/$(TARGET)/tools
COMPILER := gcc
ifeq ($(EMMC_BOOT), 1)
APPSBOOTHDR_FILES := EMMCBOOT.MBN
else
ifeq ($(BUILD_NANDWRITE), 1)
APPSBOOTHDR_FILES :=
else
APPSBOOTHDR_FILES := appsboot.mbn
endif
endif
APPSBOOTHEADER: $(APPSBOOTHDR_FILES)
appsboot.mbn: appsboothd.mbn $(OUTBIN)
cp $(OUTBIN) $(APPSBOOTHEADER_DIR)/appsboot.raw
cat $(APPSBOOTHEADER_DIR)/appsboothd.mbn $(OUTBIN) > $(APPSBOOTHEADER_DIR)/appsboot.mbn
rm -f $(APPSBOOTHEADER_DIR)/appsboothd.mbn
appsboothd.mbn: mkheader $(OUTBIN)
$(SRC_DIR)/mkheader $(OUTBIN) $(APPSBOOTHEADER_DIR)/appsboothd.mbn
EMMCBOOT.MBN: emmc_appsboothd.mbn $(OUTBIN)
cp $(OUTBIN) $(APPSBOOTHEADER_DIR)/emmc_appsboot.raw
cat $(APPSBOOTHEADER_DIR)/emmc_appsboothd.mbn $(OUTBIN) > $(APPSBOOTHEADER_DIR)/EMMCBOOT.MBN
cat $(APPSBOOTHEADER_DIR)/emmc_appsboothd.mbn $(OUTBIN) > $(APPSBOOTHEADER_DIR)/emmc_appsboot.mbn
rm -f $(APPSBOOTHEADER_DIR)/emmc_appsboothd.mbn
emmc_appsboothd.mbn: mkheader $(OUTBIN)
$(SRC_DIR)/mkheader $(OUTBIN) $(APPSBOOTHEADER_DIR)/emmc_appsboothd.mbn
mkheader: $(SRC_DIR)/mkheader.c
${COMPILER} -DMEMBASE=$(MEMBASE) $(SRC_DIR)/mkheader.c -o $(SRC_DIR)/mkheader

View File

@@ -0,0 +1,344 @@
/*
* Copyright (c) 2007, Google Inc.
* All rights reserved.
*
* Copyright (c) 2009-2011, The Linux Foundation. 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 Google, 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 BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
int print_usage(){
fprintf(stderr,"usage: mkheader <bin> <hdr> <none|unified-boot>\n");
fprintf(stderr," mkheader <bin> <hdr> <unsecure-boot>"
" <outbin>\n");
fprintf(stderr," mkheader <bin> <hdr> <secure-boot> <outbin>"
" <maxsize>\n");
fprintf(stderr," mkheader <bin> <hdr> <secure-boot> <outbin>"
" <maxsize> <certchain> <files...>\n\n");
fprintf(stderr,"bin: Input raw appsbl binary\n");
fprintf(stderr,"hdr: Output of appsbl header location\n");
fprintf(stderr,"outbin: Output of the signed or unsigned"
" apps boot location\n");
fprintf(stderr,"maxsize: Maximum size for certificate"
" chain\n");
fprintf(stderr,"certchain: Output of the certchain location\n");
fprintf(stderr,"files: Input format <bin signature>"
" <certifcate file(s) for certificate chain>...\n");
fprintf(stderr,"certificate chain: Files will be concatenated in order"
" to create the certificate chain\n\n");
return -1;
}
int cat(FILE * in, FILE * out, unsigned size, unsigned buff_size){
unsigned bytes_left = size;
char buf[buff_size];
int ret = 0;
while(bytes_left){
fread(buf, sizeof(char), buff_size, in);
if(!feof(in)){
bytes_left -= fwrite(buf, sizeof(char), buff_size, out);
}
else
bytes_left = 0;
}
ret = ferror(in) | ferror(out);
if(ret)
fprintf(stderr, "ERROR: Occured during file concatenation\n");
return ret;
}
int main(int argc, char *argv[])
{
struct stat s;
unsigned size, base;
int unified_boot = 0;
unsigned unified_boot_magic[20];
unsigned non_unified_boot_magic[10];
unsigned magic_len = 0;
unsigned *magic;
unsigned cert_chain_size = 0;
unsigned signature_size = 0;
int secure_boot = 0;
int fd;
if(argc < 3) {
return print_usage();
}
if(argc == 4) {
if(!strcmp("unified-boot",argv[3])) {
unified_boot = 1;
}
else if(!strcmp("secure-boot",argv[3])){
fprintf(stderr,
"ERROR: Missing arguments: [outbin maxsize] |"
" [outbin, maxsize, certchain,"
" signature + certifcate(s)]\n");
return print_usage();
}
else if(!strcmp("unsecure-boot",argv[3])){
fprintf(stderr,"ERROR: Missing arguments:"
" outbin directory\n");
return print_usage();
}
}
if(argc > 4) {
if(!strcmp("secure-boot",argv[3])) {
if(argc < 9 && argc != 6){
fprintf(stderr,
"ERROR: Missing argument(s):"
" [outbin maxsize] | [outbin, maxsize,"
" certchain,"
" signature + certifcate(s)]\n");
return print_usage();
}
secure_boot = 1;
signature_size = 256; //Support SHA 256
cert_chain_size = atoi(argv[5]);
}
}
if(stat(argv[1], &s)) {
perror("cannot stat binary");
return -1;
}
if(unified_boot) {
magic = unified_boot_magic;
magic_len = sizeof(unified_boot_magic);
}
else {
magic = non_unified_boot_magic;
magic_len = sizeof(non_unified_boot_magic);
}
size = s.st_size;
#if MEMBASE
base = MEMBASE;
#else
base = 0;
#endif
printf("Image Destination Pointer: 0x%x\n", base);
magic[0] = 0x00000005; /* appsbl */
magic[1] = 0x00000003; //Flash_partition_version /* nand */
magic[2] = 0x00000000; //image source pointer
magic[3] = base; //image destination pointer
magic[4] = size + cert_chain_size + signature_size; //image size
magic[5] = size; //code size
magic[6] = base + size;
magic[7] = signature_size;
magic[8] = size + base + signature_size;
magic[9] = cert_chain_size;
if(unified_boot == 1)
{
magic[10] = 0x33836685; /* cookie magic number */
magic[11] = 0x00000001; /* cookie version */
magic[12] = 0x00000002; /* file formats */
magic[13] = 0x00000000;
magic[14] = 0x00000000; /* not setting size for boot.img */
magic[15] = 0x00000000;
magic[16] = 0x00000000;
magic[17] = 0x00000000;
magic[18] = 0x00000000;
magic[19] = 0x00000000;
}
fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(fd < 0) {
perror("cannot open header for writing");
return -1;
}
if(write(fd, magic, magic_len) != magic_len) {
perror("cannot write header");
close(fd);
unlink(argv[2]);
return -1;
}
close(fd);
if(secure_boot && argc > 6){
FILE * input_file;
FILE * output_file;
unsigned buff_size = 1;
char buf[buff_size];
unsigned bytes_left;
unsigned current_cert_chain_size = 0;
int padding_size = 0;
int i;
if((output_file = fopen(argv[6], "wb"))==NULL){
perror("ERROR: Occured during fopen");
return -1;
}
printf("Certificate Chain Output File: %s\n", argv[6]);
for(i = 8; i < argc; i++){
if((input_file = fopen(argv[i], "rb"))==NULL){
perror("ERROR: Occured during fopen");
return -1;
}
stat(argv[i], &s);
bytes_left = s.st_size;
current_cert_chain_size += bytes_left;
if(cat(input_file, output_file, bytes_left, buff_size))
return -1;
fclose(input_file);
}
//Pad certifcate chain to the max expected size from input
memset(buf, 0xFF, sizeof(buf));
padding_size = cert_chain_size - current_cert_chain_size;
if(padding_size <0){
fprintf(stderr, "ERROR: Input certificate chain"
" (Size=%d) is larger than the maximum"
" specified (Size=%d)\n",
current_cert_chain_size, cert_chain_size);
return -1;
}
bytes_left = (padding_size > 0) ? padding_size : 0;
while(bytes_left){
if(!ferror(output_file))
bytes_left -= fwrite(buf,
sizeof(buf),
buff_size,
output_file);
else{
fprintf(stderr, "ERROR: Occured during"
" certifcate chain padding\n");
return -1;
}
}
fclose(output_file);
/* Concat and combine to signed image.
* Format [HDR][RAW APPSBOOT][PADDED CERT CHAIN]
*/
if((output_file = fopen(argv[4], "wb"))==NULL){
perror("ERROR: Occured during fopen");
return -1;
}
printf("Image Output File: %s\n", argv[4]);
//Header
if((input_file = fopen(argv[2], "rb"))==NULL){
perror("ERROR: Occured during fopen");
return -1;
}
stat(argv[2], &s);
if(cat(input_file, output_file, s.st_size, buff_size))
return -1;
fclose(input_file);
//Raw Appsbl
if((input_file = fopen(argv[1], "rb"))==NULL){
perror("ERROR: Occured during fopen");
return -1;
}
stat(argv[1], &s);
if(cat(input_file, output_file, s.st_size, buff_size))
return -1;
fclose(input_file);
//Signature
if((input_file = fopen(argv[7], "rb"))==NULL){
perror("ERROR: Occured during fopen");
return -1;
}
stat(argv[7], &s);
if(cat(input_file, output_file, s.st_size, buff_size))
return -1;
fclose(input_file);
//Certifcate Chain
if((input_file = fopen(argv[6], "rb"))==NULL){
perror("ERROR: Occured during fopen");
return -1;
}
if(cat(input_file, output_file,
(current_cert_chain_size + padding_size), buff_size))
return -1;
fclose(input_file);
fclose(output_file);
}
else if(argc == 5 || argc == 6){
FILE * input_file;
FILE * output_file;
unsigned buff_size = 1;
char buf[buff_size];
/* Concat and combine to unsigned image.
* Format [HDR][RAW APPSBOOT]
*/
if((output_file = fopen(argv[4], "wb"))==NULL){
perror("ERROR: Occured during fopen");
return -1;
}
printf("Image Output File: %s\n", argv[4]);
//Header
if((input_file = fopen(argv[2], "rb"))==NULL){
perror("ERROR: Occured during fopen");
return -1;
}
stat(argv[2], &s);
if(cat(input_file, output_file, s.st_size, buff_size))
return -1;
fclose(input_file);
//Raw Appsbl
if((input_file = fopen(argv[1], "rb"))==NULL){
perror("ERROR: Occured during fopen");
return -1;
}
stat(argv[1], &s);
if(cat(input_file, output_file, s.st_size, buff_size))
return -1;
fclose(input_file);
fclose(output_file);
}
printf("Done execution\n");
return 0;
}