blob: 647443b819d0b3f18ed3595334df626c4c4e4b42 [file] [log] [blame]
micky387e96abca2019-06-11 02:35:20 +02001/*
Alexander Koskovich3e4efc52022-01-22 22:22:06 -07002 * Copyright (c) 2013,2016,2020-2022 The Linux Foundation. All rights reserved.
micky387e96abca2019-06-11 02:35:20 +02003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef __GPT_UTILS_H__
31#define __GPT_UTILS_H__
32#include <vector>
33#include <string>
34#include <map>
35#ifdef __cplusplus
36extern "C" {
37#endif
38#include <unistd.h>
39#include <stdlib.h>
40/******************************************************************************
41 * GPT HEADER DEFINES
42 ******************************************************************************/
43#define GPT_SIGNATURE "EFI PART"
44#define HEADER_SIZE_OFFSET 12
45#define HEADER_CRC_OFFSET 16
46#define PRIMARY_HEADER_OFFSET 24
47#define BACKUP_HEADER_OFFSET 32
48#define FIRST_USABLE_LBA_OFFSET 40
49#define LAST_USABLE_LBA_OFFSET 48
50#define PENTRIES_OFFSET 72
51#define PARTITION_COUNT_OFFSET 80
52#define PENTRY_SIZE_OFFSET 84
53#define PARTITION_CRC_OFFSET 88
54
55#define TYPE_GUID_OFFSET 0
56#define TYPE_GUID_SIZE 16
57#define PTN_ENTRY_SIZE 128
58#define UNIQUE_GUID_OFFSET 16
59#define FIRST_LBA_OFFSET 32
60#define LAST_LBA_OFFSET 40
61#define ATTRIBUTE_FLAG_OFFSET 48
62#define PARTITION_NAME_OFFSET 56
63#define MAX_GPT_NAME_SIZE 72
64
65/******************************************************************************
66 * AB RELATED DEFINES
67 ******************************************************************************/
68//Bit 48 onwords in the attribute field are the ones where we are allowed to
69//store our AB attributes.
70#define AB_FLAG_OFFSET (ATTRIBUTE_FLAG_OFFSET + 6)
71#define GPT_DISK_INIT_MAGIC 0xABCD
72#define AB_PARTITION_ATTR_SLOT_ACTIVE (0x1<<2)
73#define AB_PARTITION_ATTR_BOOT_SUCCESSFUL (0x1<<6)
74#define AB_PARTITION_ATTR_UNBOOTABLE (0x1<<7)
75#define AB_SLOT_ACTIVE_VAL 0x3F
76#define AB_SLOT_INACTIVE_VAL 0x0
77#define AB_SLOT_ACTIVE 1
78#define AB_SLOT_INACTIVE 0
79#define AB_SLOT_A_SUFFIX "_a"
80#define AB_SLOT_B_SUFFIX "_b"
81#define PTN_XBL "xbl"
82#define PTN_XBL_CFG "xbl_config"
Siddeswar Aluganti3bf79b52021-04-14 12:04:04 -070083#define PTN_MULTIIMGOEM "multiimgoem"
84#define PTN_MULTIIMGQTI "multiimgqti"
85#define PTN_SWAP_LIST PTN_XBL, PTN_XBL_CFG, PTN_MULTIIMGOEM, PTN_MULTIIMGQTI, \
Alexander Koskoviche05eaed2021-09-03 07:16:19 -070086 "abl", "aop", "bluetooth" "cpucp" "devcfg", "dsp", "dtbo", "featenabler", \
87 "hyp", "keymaster", "multiimgoem", "qupfw", "qweslicstore", "shrm", "tz", \
Alexander Koskovich617a8d52021-09-06 19:59:06 -070088 "uefisecapp", "vbmeta_system", "vbmeta"
Alexander Koskoviche05eaed2021-09-03 07:16:19 -070089
90#define AB_PTN_LIST PTN_SWAP_LIST, \
91 "boot", "modem", "odm", "product", "system", "system_ext", "vendor", \
Roopesh Nataraja2ed94d82021-05-25 14:34:41 -070092 "vendor_dlkm", "vendor_boot", "recovery"
Alexander Koskoviche05eaed2021-09-03 07:16:19 -070093
micky387e96abca2019-06-11 02:35:20 +020094#define BOOT_DEV_DIR "/dev/block/bootdevice/by-name"
95
96/******************************************************************************
97 * HELPER MACROS
98 ******************************************************************************/
99#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
100/******************************************************************************
101 * TYPES
102 ******************************************************************************/
103enum boot_update_stage {
104 UPDATE_MAIN = 1,
105 UPDATE_BACKUP,
106 UPDATE_FINALIZE
107};
108
109enum gpt_instance {
110 PRIMARY_GPT = 0,
111 SECONDARY_GPT
112};
113
114enum boot_chain {
115 NORMAL_BOOT = 0,
116 BACKUP_BOOT
117};
118
119struct gpt_disk {
120 //GPT primary header
121 uint8_t *hdr;
122 //primary header crc
123 uint32_t hdr_crc;
124 //GPT backup header
125 uint8_t *hdr_bak;
126 //backup header crc
127 uint32_t hdr_bak_crc;
128 //Partition entries array
129 uint8_t *pentry_arr;
130 //Partition entries array for backup table
131 uint8_t *pentry_arr_bak;
132 //Size of the pentry array
133 uint32_t pentry_arr_size;
134 //Size of each element in the pentry array
135 uint32_t pentry_size;
136 //CRC of the partition entry array
137 uint32_t pentry_arr_crc;
138 //CRC of the backup partition entry array
139 uint32_t pentry_arr_bak_crc;
140 //Path to block dev representing the disk
141 char devpath[PATH_MAX];
142 //Block size of disk
143 uint32_t block_size;
144 uint32_t is_initialized;
145};
146
147/******************************************************************************
148 * FUNCTION PROTOTYPES
149 ******************************************************************************/
150int prepare_boot_update(enum boot_update_stage stage);
151//GPT disk methods
152struct gpt_disk* gpt_disk_alloc();
153//Free previously allocated gpt_disk struct
154void gpt_disk_free(struct gpt_disk *disk);
155//Get the details of the disk holding the partition whose name
156//is passed in via dev
157int gpt_disk_get_disk_info(const char *dev, struct gpt_disk *disk);
158
159//Get pointer to partition entry from a allocated gpt_disk structure
160uint8_t* gpt_disk_get_pentry(struct gpt_disk *disk,
161 const char *partname,
162 enum gpt_instance instance);
163
164//Update the crc fields of the modified disk structure
165int gpt_disk_update_crc(struct gpt_disk *disk);
166
167//Write the contents of struct gpt_disk back to the actual disk
168int gpt_disk_commit(struct gpt_disk *disk);
169
170//Return if the current device is UFS based or not
171int gpt_utils_is_ufs_device();
172
173//Swtich betwieen using either the primary or the backup
174//boot LUN for boot. This is required since UFS boot partitions
175//cannot have a backup GPT which is what we use for failsafe
176//updates of the other 'critical' partitions. This function will
177//not be invoked for emmc targets and on UFS targets is only required
178//to be invoked for XBL.
179//
180//The algorithm to do this is as follows:
181//- Find the real block device(eg: /dev/block/sdb) that corresponds
182// to the /dev/block/bootdevice/by-name/xbl(bak) symlink
183//
184//- Once we have the block device 'node' name(sdb in the above example)
185// use this node to to locate the scsi generic device that represents
186// it by checking the file /sys/block/sdb/device/scsi_generic/sgY
187//
188//- Once we locate sgY we call the query ioctl on /dev/sgy to switch
189//the boot lun to either LUNA or LUNB
190int gpt_utils_set_xbl_boot_partition(enum boot_chain chain);
191
192//Given a vector of partition names as a input and a reference to a map,
193//populate the map to indicate which physical disk each of the partitions
194//sits on. The key in the map is the path to the block device where the
195//partiton lies and the value is a vector of strings indicating which of
196//the passed in partiton names sits on that device.
197int gpt_utils_get_partition_map(std::vector<std::string>& partition_list,
198 std::map<std::string,std::vector<std::string>>& partition_map);
199#ifdef __cplusplus
200}
201#endif
202#endif /* __GPT_UTILS_H__ */