blob: 25999984df19dd58468df4f2fbdbc9798fc36bf7 [file] [log] [blame]
wdenk012771d2002-03-08 21:31:05 +00001/*
wdenkc35ba4e2004-03-14 22:25:36 +00002 * (C) Copyright 2000-2004
wdenk012771d2002-03-08 21:31:05 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk012771d2002-03-08 21:31:05 +00006 */
7#ifndef _PART_H
8#define _PART_H
Grant Likelyffc2dd72007-02-20 09:04:34 +01009
Simon Glassac8162f2016-02-29 15:25:39 -070010#include <blk.h>
wdenk05939202004-04-18 17:39:38 +000011#include <ide.h>
wdenk012771d2002-03-08 21:31:05 +000012
Egbert Eich2eec2ab2013-04-09 21:11:56 +000013#define LOG2(x) (((x & 0xaaaaaaaa) ? 1 : 0) + ((x & 0xcccccccc) ? 2 : 0) + \
14 ((x & 0xf0f0f0f0) ? 4 : 0) + ((x & 0xff00ff00) ? 8 : 0) + \
15 ((x & 0xffff0000) ? 16 : 0))
16#define LOG2_INVALID(type) ((type)((sizeof(type)<<3)-1))
Egbert Eich071e4232013-04-09 06:03:36 +000017
wdenk012771d2002-03-08 21:31:05 +000018/* Part types */
wdenkc35ba4e2004-03-14 22:25:36 +000019#define PART_TYPE_UNKNOWN 0x00
wdenk012771d2002-03-08 21:31:05 +000020#define PART_TYPE_MAC 0x01
21#define PART_TYPE_DOS 0x02
22#define PART_TYPE_ISO 0x03
wdenkc35ba4e2004-03-14 22:25:36 +000023#define PART_TYPE_AMIGA 0x04
richardretanubune6745592008-09-26 11:13:22 -040024#define PART_TYPE_EFI 0x05
wdenk452cfd62002-11-19 11:04:11 +000025
wdenkf12e3962003-06-29 21:03:46 +000026/*
27 * Type string for U-Boot bootable partitions
28 */
29#define BOOT_PART_TYPE "U-Boot" /* primary boot partition type */
30#define BOOT_PART_COMP "PPCBoot" /* PPCBoot compatibility type */
31
wdenk012771d2002-03-08 21:31:05 +000032/* device types */
wdenkf12e3962003-06-29 21:03:46 +000033#define DEV_TYPE_UNKNOWN 0xff /* not connected */
34#define DEV_TYPE_HARDDISK 0x00 /* harddisk */
wdenkc35ba4e2004-03-14 22:25:36 +000035#define DEV_TYPE_TAPE 0x01 /* Tape */
36#define DEV_TYPE_CDROM 0x05 /* CD-ROM */
37#define DEV_TYPE_OPDISK 0x07 /* optical disk */
wdenk012771d2002-03-08 21:31:05 +000038
wdenkc35ba4e2004-03-14 22:25:36 +000039typedef struct disk_partition {
Frederic Leroye7ee0282013-06-26 18:11:25 +020040 lbaint_t start; /* # of first block in partition */
41 lbaint_t size; /* number of blocks in partition */
wdenk012771d2002-03-08 21:31:05 +000042 ulong blksz; /* block size in bytes */
43 uchar name[32]; /* partition name */
44 uchar type[32]; /* string type description */
Rob Herringeda51112012-08-23 11:31:43 +000045 int bootable; /* Active/Bootable flag is set */
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +000046#ifdef CONFIG_PARTITION_UUIDS
47 char uuid[37]; /* filesystem UUID as string, if exists */
48#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +010049#ifdef CONFIG_PARTITION_TYPE_GUID
50 char type_guid[37]; /* type GUID as string, if exists */
51#endif
wdenk012771d2002-03-08 21:31:05 +000052} disk_partition_t;
53
Grant Likelyffc2dd72007-02-20 09:04:34 +010054/* Misc _get_dev functions */
Matthew McClintock6252b4f2011-05-24 05:31:19 +000055#ifdef CONFIG_PARTITIONS
Simon Glasse3394752016-02-29 15:25:34 -070056struct blk_desc *get_dev(const char *ifname, int dev);
57struct blk_desc *ide_get_dev(int dev);
58struct blk_desc *sata_get_dev(int dev);
59struct blk_desc *scsi_get_dev(int dev);
60struct blk_desc *usb_stor_get_dev(int dev);
61struct blk_desc *mmc_get_dev(int dev);
Stephen Warren52c44e42014-05-07 12:19:02 -060062int mmc_select_hwpart(int dev_num, int hwpart);
Simon Glasse3394752016-02-29 15:25:34 -070063struct blk_desc *systemace_get_dev(int dev);
64struct blk_desc *mg_disk_get_dev(int dev);
65struct blk_desc *host_get_dev(int dev);
66int host_get_dev_err(int dev, struct blk_desc **blk_devp);
Grant Likelyffc2dd72007-02-20 09:04:34 +010067
wdenk012771d2002-03-08 21:31:05 +000068/* disk/part.c */
Simon Glasse3394752016-02-29 15:25:34 -070069int get_partition_info(struct blk_desc *dev_desc, int part,
70 disk_partition_t *info);
71void print_part(struct blk_desc *dev_desc);
72void init_part(struct blk_desc *dev_desc);
73void dev_print(struct blk_desc *dev_desc);
Stephen Warrenafd909c2012-09-21 09:50:56 +000074int get_device(const char *ifname, const char *dev_str,
Simon Glasse3394752016-02-29 15:25:34 -070075 struct blk_desc **dev_desc);
Stephen Warrenc87ff222012-09-21 09:50:57 +000076int get_device_and_partition(const char *ifname, const char *dev_part_str,
Simon Glasse3394752016-02-29 15:25:34 -070077 struct blk_desc **dev_desc,
Stephen Warrenc87ff222012-09-21 09:50:57 +000078 disk_partition_t *info, int allow_whole_dev);
Matthew McClintock6252b4f2011-05-24 05:31:19 +000079#else
Simon Glasse3394752016-02-29 15:25:34 -070080static inline struct blk_desc *get_dev(const char *ifname, int dev)
Rob Herringd586cec2012-09-21 04:08:17 +000081{ return NULL; }
Simon Glasse3394752016-02-29 15:25:34 -070082static inline struct blk_desc *ide_get_dev(int dev) { return NULL; }
83static inline struct blk_desc *sata_get_dev(int dev) { return NULL; }
84static inline struct blk_desc *scsi_get_dev(int dev) { return NULL; }
85static inline struct blk_desc *usb_stor_get_dev(int dev) { return NULL; }
86static inline struct blk_desc *mmc_get_dev(int dev) { return NULL; }
Stephen Warren52c44e42014-05-07 12:19:02 -060087static inline int mmc_select_hwpart(int dev_num, int hwpart) { return -1; }
Simon Glasse3394752016-02-29 15:25:34 -070088static inline struct blk_desc *systemace_get_dev(int dev) { return NULL; }
89static inline struct blk_desc *mg_disk_get_dev(int dev) { return NULL; }
90static inline struct blk_desc *host_get_dev(int dev) { return NULL; }
wdenk012771d2002-03-08 21:31:05 +000091
Simon Glasse3394752016-02-29 15:25:34 -070092static inline int get_partition_info(struct blk_desc *dev_desc, int part,
93 disk_partition_t *info) { return -1; }
94static inline void print_part(struct blk_desc *dev_desc) {}
95static inline void init_part(struct blk_desc *dev_desc) {}
96static inline void dev_print(struct blk_desc *dev_desc) {}
Stephen Warrenafd909c2012-09-21 09:50:56 +000097static inline int get_device(const char *ifname, const char *dev_str,
Simon Glasse3394752016-02-29 15:25:34 -070098 struct blk_desc **dev_desc)
Stephen Warrenafd909c2012-09-21 09:50:56 +000099{ return -1; }
Rob Herringd586cec2012-09-21 04:08:17 +0000100static inline int get_device_and_partition(const char *ifname,
Stephen Warrenc87ff222012-09-21 09:50:57 +0000101 const char *dev_part_str,
Simon Glasse3394752016-02-29 15:25:34 -0700102 struct blk_desc **dev_desc,
Stephen Warrenc87ff222012-09-21 09:50:57 +0000103 disk_partition_t *info,
104 int allow_whole_dev)
Rob Herringd586cec2012-09-21 04:08:17 +0000105{ *dev_desc = NULL; return -1; }
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000106#endif
wdenk012771d2002-03-08 21:31:05 +0000107
108#ifdef CONFIG_MAC_PARTITION
109/* disk/part_mac.c */
Simon Glasse3394752016-02-29 15:25:34 -0700110int get_partition_info_mac(struct blk_desc *dev_desc, int part,
111 disk_partition_t *info);
112void print_part_mac(struct blk_desc *dev_desc);
113int test_part_mac(struct blk_desc *dev_desc);
wdenk012771d2002-03-08 21:31:05 +0000114#endif
115
116#ifdef CONFIG_DOS_PARTITION
117/* disk/part_dos.c */
Simon Glasse3394752016-02-29 15:25:34 -0700118int get_partition_info_dos(struct blk_desc *dev_desc, int part,
119 disk_partition_t *info);
120void print_part_dos(struct blk_desc *dev_desc);
121int test_part_dos(struct blk_desc *dev_desc);
wdenk012771d2002-03-08 21:31:05 +0000122#endif
123
124#ifdef CONFIG_ISO_PARTITION
125/* disk/part_iso.c */
Simon Glasse3394752016-02-29 15:25:34 -0700126int get_partition_info_iso(struct blk_desc *dev_desc, int part,
127 disk_partition_t *info);
128void print_part_iso(struct blk_desc *dev_desc);
129int test_part_iso(struct blk_desc *dev_desc);
wdenk012771d2002-03-08 21:31:05 +0000130#endif
131
wdenk452cfd62002-11-19 11:04:11 +0000132#ifdef CONFIG_AMIGA_PARTITION
133/* disk/part_amiga.c */
Simon Glasse3394752016-02-29 15:25:34 -0700134int get_partition_info_amiga(struct blk_desc *dev_desc, int part,
135 disk_partition_t *info);
136void print_part_amiga(struct blk_desc *dev_desc);
137int test_part_amiga(struct blk_desc *dev_desc);
wdenk452cfd62002-11-19 11:04:11 +0000138#endif
139
richardretanubune6745592008-09-26 11:13:22 -0400140#ifdef CONFIG_EFI_PARTITION
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100141#include <part_efi.h>
richardretanubune6745592008-09-26 11:13:22 -0400142/* disk/part_efi.c */
Simon Glasse3394752016-02-29 15:25:34 -0700143int get_partition_info_efi(struct blk_desc *dev_desc, int part,
144 disk_partition_t *info);
Steve Raef7f10b82014-05-26 11:52:24 -0700145/**
146 * get_partition_info_efi_by_name() - Find the specified GPT partition table entry
147 *
148 * @param dev_desc - block device descriptor
149 * @param gpt_name - the specified table entry name
150 * @param info - returns the disk partition info
151 *
152 * @return - '0' on match, '-1' on no match, otherwise error
153 */
Simon Glasse3394752016-02-29 15:25:34 -0700154int get_partition_info_efi_by_name(struct blk_desc *dev_desc,
Steve Raef7f10b82014-05-26 11:52:24 -0700155 const char *name, disk_partition_t *info);
Simon Glasse3394752016-02-29 15:25:34 -0700156void print_part_efi(struct blk_desc *dev_desc);
157int test_part_efi(struct blk_desc *dev_desc);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100158
159/**
160 * write_gpt_table() - Write the GUID Partition Table to disk
161 *
162 * @param dev_desc - block device descriptor
163 * @param gpt_h - pointer to GPT header representation
164 * @param gpt_e - pointer to GPT partition table entries
165 *
166 * @return - zero on success, otherwise error
167 */
Simon Glasse3394752016-02-29 15:25:34 -0700168int write_gpt_table(struct blk_desc *dev_desc,
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100169 gpt_header *gpt_h, gpt_entry *gpt_e);
170
171/**
172 * gpt_fill_pte(): Fill the GPT partition table entry
173 *
174 * @param gpt_h - GPT header representation
175 * @param gpt_e - GPT partition table entries
176 * @param partitions - list of partitions
177 * @param parts - number of partitions
178 *
179 * @return zero on success
180 */
181int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
182 disk_partition_t *partitions, int parts);
183
184/**
185 * gpt_fill_header(): Fill the GPT header
186 *
187 * @param dev_desc - block device descriptor
188 * @param gpt_h - GPT header representation
189 * @param str_guid - disk guid string representation
190 * @param parts_count - number of partitions
191 *
192 * @return - error on str_guid conversion error
193 */
Simon Glasse3394752016-02-29 15:25:34 -0700194int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h,
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100195 char *str_guid, int parts_count);
196
197/**
198 * gpt_restore(): Restore GPT partition table
199 *
200 * @param dev_desc - block device descriptor
201 * @param str_disk_guid - disk GUID
202 * @param partitions - list of partitions
203 * @param parts - number of partitions
204 *
205 * @return zero on success
206 */
Simon Glasse3394752016-02-29 15:25:34 -0700207int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid,
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100208 disk_partition_t *partitions, const int parts_count);
Steve Rae7d059342014-12-12 15:51:54 -0800209
210/**
211 * is_valid_gpt_buf() - Ensure that the Primary GPT information is valid
212 *
213 * @param dev_desc - block device descriptor
214 * @param buf - buffer which contains the MBR and Primary GPT info
215 *
216 * @return - '0' on success, otherwise error
217 */
Simon Glasse3394752016-02-29 15:25:34 -0700218int is_valid_gpt_buf(struct blk_desc *dev_desc, void *buf);
Steve Rae7d059342014-12-12 15:51:54 -0800219
220/**
221 * write_mbr_and_gpt_partitions() - write MBR, Primary GPT and Backup GPT
222 *
223 * @param dev_desc - block device descriptor
224 * @param buf - buffer which contains the MBR and Primary GPT info
225 *
226 * @return - '0' on success, otherwise error
227 */
Simon Glasse3394752016-02-29 15:25:34 -0700228int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100229
230/**
231 * gpt_verify_headers() - Function to read and CRC32 check of the GPT's header
232 * and partition table entries (PTE)
233 *
234 * As a side effect if sets gpt_head and gpt_pte so they point to GPT data.
235 *
236 * @param dev_desc - block device descriptor
237 * @param gpt_head - pointer to GPT header data read from medium
238 * @param gpt_pte - pointer to GPT partition table enties read from medium
239 *
240 * @return - '0' on success, otherwise error
241 */
Simon Glasse3394752016-02-29 15:25:34 -0700242int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100243 gpt_entry **gpt_pte);
244
245/**
246 * gpt_verify_partitions() - Function to check if partitions' name, start and
247 * size correspond to '$partitions' env variable
248 *
249 * This function checks if on medium stored GPT data is in sync with information
250 * provided in '$partitions' environment variable. Specificially, name, start
251 * and size of the partition is checked.
252 *
253 * @param dev_desc - block device descriptor
254 * @param partitions - partition data read from '$partitions' env variable
255 * @param parts - number of partitions read from '$partitions' env variable
256 * @param gpt_head - pointer to GPT header data read from medium
257 * @param gpt_pte - pointer to GPT partition table enties read from medium
258 *
259 * @return - '0' on success, otherwise error
260 */
Simon Glasse3394752016-02-29 15:25:34 -0700261int gpt_verify_partitions(struct blk_desc *dev_desc,
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100262 disk_partition_t *partitions, int parts,
263 gpt_header *gpt_head, gpt_entry **gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400264#endif
265
wdenk012771d2002-03-08 21:31:05 +0000266#endif /* _PART_H */