302 lines
8.6 KiB
C
302 lines
8.6 KiB
C
/* SPDX-License-Identifier: (GPL-2.0+ OR MIT) */
|
|
/*
|
|
* Copyright (c) 2019 Amlogic, Inc. All rights reserved.
|
|
*/
|
|
|
|
#ifndef _AML_MMC_H
|
|
#define _AML_MMC_H
|
|
|
|
#include <asm/io.h>
|
|
#include <config.h>
|
|
#include <command.h>
|
|
#include <mmc.h>
|
|
#include <part.h>
|
|
#include <malloc.h>
|
|
#include <linux/list.h>
|
|
#include <mmc.h>
|
|
#include <div64.h>
|
|
#include <env.h>
|
|
#include <malloc.h>
|
|
#include <asm/cache.h>
|
|
//#include <asm/amlogic/arch/clock.h>
|
|
#include <amlogic/partition_table.h>
|
|
#include <amlogic/emmc_storage.h>
|
|
#include <linux/sizes.h>
|
|
|
|
#define AML_MMC_DBG
|
|
|
|
#define MAX_DEVICE_NUM 32
|
|
#define MAX_DEVICE_NAME_LEN 16
|
|
#define MAX_MMC_PART_NUM 128
|
|
#define MAX_MMC_PART_NAME_LEN 16
|
|
#define MMC_BLOCK_SIZE (512)
|
|
|
|
#define MMC_MPT_VERSION_1 "01.00.00"
|
|
#define MMC_MPT_VERSION_2 "01.02.00"
|
|
/* version in use, fixme when kernel driver is updated. */
|
|
#define MMC_MPT_VERSION MMC_MPT_VERSION_1
|
|
|
|
#define MMC_PARTITIONS_MAGIC "MPT" // MMC Partition Table
|
|
#define MMC_CARD_PARTITION_NAME "card"
|
|
|
|
#ifndef CONFIG_AML_MMC_INHERENT_PART
|
|
/* reserved partition config */
|
|
#define MMC_RESERVED_NAME "reserved"
|
|
#ifdef CONFIG_AMLOGIC_MIN_RESERVED_PARTITION
|
|
#define PARTITION_RESERVED (0 * SZ_1M)
|
|
#define PARTITION_MIN_RESERVED 0
|
|
#define MMC_BOOT_PARTITION_RESERVED (0 * SZ_1M)
|
|
#define RESERVED_GPT_OFFSET (4 * SZ_1M)
|
|
#define MMC_RESERVED_SIZE (2 * SZ_1M)
|
|
#else
|
|
#define PARTITION_RESERVED (8 * SZ_1M)
|
|
#define PARTITION_MIN_RESERVED SZ_1M
|
|
#define MMC_BOOT_PARTITION_RESERVED (32 * SZ_1M)
|
|
#define RESERVED_GPT_OFFSET (36 * SZ_1M)
|
|
#define MMC_RESERVED_SIZE (64 * SZ_1M)
|
|
#endif /* CONFIG_AMLOGIC_MIN_RESERVED_PARTITION */
|
|
#define MMC_BOTTOM_RSV_SIZE (0)
|
|
|
|
#define MMC_BOOT_NAME "bootloader"
|
|
#define MMC_BOOT_NAME0 "bootloader-boot0"
|
|
#define MMC_BOOT_NAME1 "bootloader-boot1"
|
|
#define MMC_BOOT_DEVICE_SIZE (0x4 * SZ_1M)
|
|
#endif /* CONFIG_AML_MMC_INHERENT_PART */
|
|
|
|
/* reserved partition table */
|
|
/* <----table----> */
|
|
#define MMC_TABLE_NAME ("AML_TABLE")
|
|
#define MMC_TABLE_OFFSET (0x0)
|
|
#define MMC_TABLE_SIZE (16 * SZ_1K)
|
|
/* <----key----> */
|
|
#define MMC_KEY_NAME "key"
|
|
#define EMMCKEY_RESERVE_OFFSET (16 * SZ_1K)
|
|
#define MMC_KEY_SIZE (256 * SZ_1K)
|
|
#define KEY_BACKUP
|
|
#define KEY_COPIES (2)
|
|
/* <----pattern----> */
|
|
#define MMC_PATTERN_NAME "pattern"
|
|
#ifdef CONFIG_AMLOGIC_MIN_RESERVED_PARTITION
|
|
#define CALI_PATTERN_OFFSET (528 * SZ_1K)
|
|
#else
|
|
#define CALI_PATTERN_OFFSET (SZ_1M * 3)
|
|
#endif /* CONFIG_AMLOGIC_MIN_RESERVED_PARTITION */
|
|
#define CALI_PATTERN_ADDR (RESERVED_GPT_OFFSET + CALI_PATTERN_OFFSET)
|
|
#define CALI_PATTERN_SIZE (256 * 512)
|
|
#define CALI_BLOCK_SIZE (512)
|
|
#define CALI_PATTERN (0x55aa55aa)
|
|
/* <----magic----> */
|
|
#define MMC_MAGIC_NAME "magic"
|
|
#ifdef CONFIG_AMLOGIC_MIN_RESERVED_PARTITION
|
|
#define MAGIC_OFFSET (656 * SZ_1K)
|
|
#else
|
|
#define MAGIC_OFFSET (SZ_1M * 6)
|
|
#endif /* CONFIG_AMLOGIC_MIN_RESERVED_PARTITION */
|
|
#define MAGIC_ADDR (RESERVED_GPT_OFFSET + MAGIC_OFFSET)
|
|
#define MAGIC_SIZE (256 * 512)
|
|
#define MAGIC_BLOCK_SIZE (512)
|
|
#define MAGIC_PATTERN (0X00FF00FF)
|
|
/* <----random----> */
|
|
#define MMC_RANDOM_NAME "random"
|
|
#ifdef CONFIG_AMLOGIC_MIN_RESERVED_PARTITION
|
|
#define RANDOM_OFFSET (784 * SZ_1K)
|
|
#else
|
|
#define RANDOM_OFFSET (SZ_1M * 7)
|
|
#endif /* CONFIG_AMLOGIC_MIN_RESERVED_PARTITION */
|
|
#define RANDOM_ADDR (RESERVED_GPT_OFFSET + RANDOM_OFFSET)
|
|
#define RANDOM_SIZE (256 * 512)
|
|
#define RANDOM_BLOCK_SIZE (512)
|
|
#define RANDOM_PATTERN (0X52414E44)
|
|
/*
|
|
* 2 copies dtb were stored in dtb area.
|
|
* timestamp&checksum are in the tail.
|
|
* |<--------------DTB Area-------------->|
|
|
* |<------DTB1------->|<------DTB2------>|
|
|
*/
|
|
#define MMC_DTB_NAME "dtb"
|
|
#ifdef CONFIG_AMLOGIC_MIN_RESERVED_PARTITION
|
|
#define DTB_OFFSET (912 * SZ_1K)
|
|
#else
|
|
#define DTB_OFFSET (SZ_1M * 4)
|
|
#endif /* CONFIG_AMLOGIC_MIN_RESERVED_PARTITION */
|
|
#define DTB_ADDR (RESERVED_GPT_OFFSET + DTB_OFFSET)
|
|
#define DTB_BLK_SIZE (512)
|
|
#define DTB_BLK_CNT (512)
|
|
#define DTB_SIZE (DTB_BLK_CNT * DTB_BLK_SIZE)
|
|
#define DTB_COPIES (2)
|
|
#define DTB_AREA_BLK_CNT (DTB_BLK_CNT * DTB_COPIES)
|
|
#define EMMC_DTB_DEV (1)
|
|
/* <----ddr parameter----> */
|
|
#define MMC_DDR_PARAMETER_NAME "ddr-parameter"
|
|
#ifdef CONFIG_AMLOGIC_MIN_RESERVED_PARTITION
|
|
#define DDR_PARAMETER_OFFSET (1424 * SZ_1K)
|
|
#else
|
|
#define DDR_PARAMETER_OFFSET (SZ_1M * 8)
|
|
#endif /* CONFIG_AMLOGIC_MIN_RESERVED_PARTITION */
|
|
#define DDR_PARAMETER_SIZE (8 * 512)
|
|
|
|
struct virtual_partition {
|
|
char name[MAX_MMC_PART_NAME_LEN];
|
|
uint64_t offset;
|
|
uint64_t size;
|
|
};
|
|
|
|
#define VIRTUAL_PARTITION_ELEMENT(na, of, sz) {.name = na, .offset = of, .size = sz,}
|
|
|
|
struct aml_pattern {
|
|
char name[MAX_MMC_PART_NAME_LEN];
|
|
unsigned int pattern;
|
|
};
|
|
#define AML_PATTERN_ELEMENT(na, pa) {.name = na, .pattern = pa,}
|
|
|
|
#ifdef AML_MMC_DBG
|
|
#define aml_mmc_dbg(fmt, ...) printk( "%s: line:%d " fmt "\n", \
|
|
__func__, __LINE__, ##__VA_ARGS__)
|
|
|
|
#define aml_mmc_msg(fmt, ...) printk( "%s: line:%d " fmt "\n", \
|
|
__func__, __LINE__, ##__VA_ARGS__)
|
|
#else
|
|
#define aml_mmc_dbg(fmt, ...)
|
|
#define aml_mmc_msg(fmt, ...) printk( fmt "\n", ##__VA_ARGS__)
|
|
#endif
|
|
|
|
#define DOS_MBR 0
|
|
#define DOS_PBR 1
|
|
|
|
#define DOS_PBR_FSTYPE_OFFSET 0x36
|
|
#define DOS_PBR32_FSTYPE_OFFSET 0x52
|
|
|
|
#define DOS_PART_DISKSIG_OFFSET 0x1b8
|
|
#define DOS_PART_TBL_OFFSET 0x1be
|
|
#define DOS_PART_MAGIC_OFFSET 0x1fe
|
|
#define DOS_PBR_MEDIA_TYPE_OFFSET 0x15
|
|
|
|
#define DOS_PARTITION_COUNT 32
|
|
|
|
#define STORAGE_EMMC 1
|
|
|
|
typedef struct dos_partition {
|
|
unsigned char boot_ind; /* 0x80 - active */
|
|
unsigned char head; /* starting head */
|
|
unsigned char sector; /* starting sector */
|
|
unsigned char cyl; /* starting cylinder */
|
|
unsigned char sys_ind; /* What partition type */
|
|
unsigned char end_head; /* end head */
|
|
unsigned char end_sector; /* end sector */
|
|
unsigned char end_cyl; /* end cylinder */
|
|
unsigned char start4[4]; /* starting sector counting from 0 */
|
|
unsigned char size4[4]; /* nr of sectors in partition */
|
|
} dos_partition_t;
|
|
|
|
struct dos_mbr_or_ebr{
|
|
unsigned char bootstart[446];
|
|
struct dos_partition part_entry[4];
|
|
unsigned char magic[2];
|
|
};
|
|
|
|
struct mmc_partitions_fmt {
|
|
char magic[4];
|
|
unsigned char version[12];
|
|
int part_num;
|
|
int checksum;
|
|
struct partitions partitions[MAX_MMC_PART_NUM];
|
|
};
|
|
|
|
struct mmc_partition_config{
|
|
unsigned char version[12];
|
|
int part_num;
|
|
struct partitions partitions[MAX_MMC_PART_NUM];
|
|
unsigned option;
|
|
void * private_data;
|
|
};
|
|
|
|
/*
|
|
struct _mmc_device{
|
|
char name[MAX_DEVICE_NAME_LEN];
|
|
uint64_t offset;
|
|
uint64_t size;
|
|
void * private_data;
|
|
struct list_head list;
|
|
};
|
|
*/
|
|
|
|
#define LOCK_MAJOR_VERSION 1
|
|
#define LOCK_MINOR_VERSION 0
|
|
|
|
#define LOCK_DATA_SIZE 16
|
|
|
|
typedef struct LockData {
|
|
uint8_t version_major;
|
|
uint8_t version_minor;
|
|
uint8_t unlock_ability;
|
|
|
|
/* Padding to eight bytes. */
|
|
uint8_t reserved1;
|
|
|
|
/* 0: unlock 1: lock*/
|
|
uint8_t lock_state;
|
|
|
|
/* 0: unlock 1: lock*/
|
|
uint8_t lock_critical_state;
|
|
|
|
/* 0: enable bootloader version rollback 1: prevent bootloader version rollback*/
|
|
uint8_t lock_bootloader;
|
|
uint8_t reserved2[1];
|
|
} LockData_t;
|
|
|
|
#define AVB_CUSTOM_KEY_LEN_MAX 4096
|
|
typedef struct AvbKey {
|
|
char magic_name[4];
|
|
uint32_t size;
|
|
} AvbKey_t;
|
|
|
|
/*512Bytes*/
|
|
typedef struct FastbootContext {
|
|
/* locks */
|
|
LockData_t lock;
|
|
uint8_t rsv[248];
|
|
/* checksum, storage driver care */
|
|
uint32_t crc32;
|
|
} FastbootContext_t;
|
|
|
|
extern struct partitions *part_table;
|
|
extern int parts_total_num;
|
|
extern struct partitions emmc_partition_table[];
|
|
|
|
extern int get_emmc_partition_arraysize(void);
|
|
|
|
/*
|
|
* get the partition number by name
|
|
* return value
|
|
* < 0 means no partition found
|
|
* >= 0 means valid partition
|
|
*/
|
|
extern int get_partition_num_by_name(char const *name);
|
|
extern int aml_gpt_valid(struct mmc *mmc);
|
|
int mmc_gpt_read(void *source);
|
|
int mmc_gpt_write(void *source);
|
|
int mmc_gpt_erase(void);
|
|
|
|
struct partitions *find_mmc_partition_by_name(char const *name);
|
|
struct partitions *aml_get_partition_by_name(const char *name);
|
|
int mmc_boot_size(char *name, uint64_t* size);
|
|
struct virtual_partition *aml_get_virtual_partition_by_name(const char *name);
|
|
bool aml_is_emmc_tsd (struct mmc *mmc);
|
|
int check_gpt_change(struct blk_desc *dev_desc, void *buf);
|
|
int mmc_device_init (struct mmc *mmc);
|
|
int get_ept_from_gpt(struct mmc *mmc);
|
|
int mmc_partition_init(void);
|
|
int aml_is_valid_gpt_buf(struct blk_desc *desc, void *buf);
|
|
int resize_gpt(struct mmc *mmc);
|
|
int check_gpt_part(struct blk_desc *dev_desc, void *buf);
|
|
int get_partition_from_gpt(unsigned char *buffer);
|
|
int mmc_key_write_backup(const char *name,
|
|
unsigned char *addr, unsigned int size);
|
|
int mmc_key_read_backup(const char *name,
|
|
unsigned char *addr, unsigned int size);
|
|
|
|
int erase_gpt_part_table(struct blk_desc *dev_desc);
|
|
#define PARTITION_ELEMENT(na, sz, flags) {.name = na, .size = sz, .mask_flags = flags,}
|
|
|
|
#endif
|