1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright (C) 2018 IBM Corp. */
3 #pragma once
4 
5 #include <stdint.h>
6 #include <sys/types.h>
7 
8 /* There are two structures outlined here - one that represents the PNOR
9  * partition table (or header) - this appears first in the PNOR image.
10  * The last field of the PNOR partition table structure is an array
11  * of another structure - which represents the partition.
12  *
13  * The flash structures used here have been borrowed from
14  * https://github.com/open-power/hostboot/blob/master/src/usr/pnor/ffs.h */
15 
16 
17 /* The maximum length of a partition's name */
18 #define PARTITION_NAME_MAX 15
19 
20 /* The version of this partition implementation. This is an
21  * incrementing value */
22 #define PARTITION_VERSION_1 1
23 
24 /* Magic number for the partition partition_table (ASCII 'PART') */
25 #define PARTITION_HEADER_MAGIC 0x50415254
26 
27 /* Default parent partition id */
28 #define PARENT_PATITION_ID 0xFFFFFFFF
29 
30 /* The partition structure has 16 'user data' words, which can be used to store
31  * miscellaneous information. This is typically used to store bits that state
32  * whether a partition is ECC protected, is read-only, is preserved across
33  * updates, etc.
34  *
35  * TODO: Replace with libflash (!) or at least refactor the data structures to
36  * better match hostboot's layout[1]. The latter would avoid the headache of
37  * verifying these flags match the expected functionality (taking into account
38  * changes in endianness).
39  *
40  * [1] https://github.com/open-power/hostboot/blob/9acfce99596f12dcc60952f8506a77e542609cbf/src/usr/pnor/common/ffs_hb.H#L81
41  */
42 #define PARTITION_USER_WORDS 16
43 #define PARTITION_ECC_PROTECTED 0x8000
44 #define PARTITION_PRESERVED 0x00800000
45 #define PARTITION_READONLY 0x00400000
46 #define PARTITION_REPROVISION 0x00100000
47 #define PARTITION_VOLATILE 0x00080000
48 #define PARTITION_CLEARECC 0x00040000
49 #define PARTITION_VERSION_CHECK_SHA512 0x80000000
50 #define PARTITION_VERSION_CHECK_SHA512_PER_EC 0x40000000
51 
52 /* Partition flags */
53 enum partition_flags {
54     PARTITION_FLAGS_PROTECTED = 0x0001,
55     PARTITION_FLAGS_U_BOOT_ENV = 0x0002
56 };
57 
58 /* Type of image contained within partition */
59 enum partition_type {
60     PARTITION_TYPE_DATA = 1,
61     PARTITION_TYPE_LOGICAL = 2,
62     PARTITION_TYPE_PARTITION = 3
63 };
64 
65 
66 /**
67  * struct pnor_partition
68  *
69  * @name:       Name of the partition - a null terminated string
70  * @base:       The offset in the PNOR, in block-size (1 block = 4KB),
71  *              where this partition is placed
72  * @size:       Partition size in blocks.
73  * @pid:        Parent partition id
74  * @id:         Partition ID [1..65536]
75  * @type:       Type of partition, see the 'type' enum
76  * @flags:      Partition flags (optional), see the 'flags' enum
77  * @actual:     Actual partition size (in bytes)
78  * @resvd:      Reserved words for future use
79  * @user:       User data (optional), see user data macros above
80  * @checksum:   Partition checksum (includes all words above) - the
81  *              checksum is obtained by a XOR operation on all of the
82  *              words above. This is used for detecting a corruption
83  *              in this structure
84  */
85 struct pnor_partition {
86     struct {
87         char         name[PARTITION_NAME_MAX + 1];
88         uint32_t     base;
89         uint32_t     size;
90         uint32_t     pid;
91         uint32_t     id;
92         uint32_t     type;
93         uint32_t     flags;
94         uint32_t     actual;
95         uint32_t     resvd[4];
96         struct
97         {
98             uint32_t data[PARTITION_USER_WORDS];
99         } user;
100     } __attribute__ ((packed)) data;
101     uint32_t     checksum;
102 } __attribute__ ((packed));
103 
104 /**
105  * struct pnor_partition_table
106  *
107  * @magic:          Eye catcher/corruption detector - set to
108  *                  PARTITION_HEADER_MAGIC
109  * @version:        Version of the structure, set to
110  *                  PARTITION_VERSION_1
111  * @size:           Size of partition table (in blocks)
112  * @entry_size:     Size of struct pnor_partition element (in bytes)
113  * @entry_count:    Number of struct pnor_partition elements in partitions array
114  * @block_size:     Size of an erase-block on the PNOR (in bytes)
115  * @block_count:    Number of blocks on the PNOR
116  * @resvd:          Reserved words for future use
117  * @checksum:       Header checksum (includes all words above) - the
118  *                  checksum is obtained by a XOR operation on all of the
119  *                  words above. This is used for detecting a corruption
120  *                  in this structure
121  * @partitions:     Array of struct pnor_partition
122  */
123 struct pnor_partition_table {
124     struct {
125         uint32_t         magic;
126         uint32_t         version;
127         uint32_t         size;
128         uint32_t         entry_size;
129         uint32_t         entry_count;
130         uint32_t         block_size;
131         uint32_t         block_count;
132         uint32_t         resvd[4];
133     } __attribute__ ((packed)) data;
134     uint32_t         checksum;
135     struct pnor_partition partitions[];
136 } __attribute__ ((packed));
137