xref: /openbmc/qemu/hw/nvram/chrp_nvram.c (revision 2024c01421eb990e7043afa0e0e4d67f4477596f)
1 /*
2  * Common Hardware Reference Platform NVRAM helper functions.
3  *
4  * The CHRP NVRAM layout is used by OpenBIOS and SLOF. See CHRP
5  * specification, chapter 8, or the LoPAPR specification for details
6  * about the NVRAM layout.
7  *
8  * This code is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published
10  * by the Free Software Foundation; either version 2 of the License,
11  * or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "qemu/osdep.h"
23 #include "qemu/cutils.h"
24 #include "hw/hw.h"
25 #include "hw/nvram/chrp_nvram.h"
26 #include "hw/nvram/openbios_firmware_abi.h"
27 #include "sysemu/sysemu.h"
28 
29 /**
30  * Create a "system partition", used for the Open Firmware
31  * environment variables.
32  */
33 int chrp_nvram_create_system_partition(uint8_t *data, int min_len)
34 {
35     struct OpenBIOS_nvpart_v1 *part_header;
36     unsigned int i;
37     int end;
38 
39     part_header = (struct OpenBIOS_nvpart_v1 *)data;
40     part_header->signature = OPENBIOS_PART_SYSTEM;
41     pstrcpy(part_header->name, sizeof(part_header->name), "system");
42 
43     end = sizeof(struct OpenBIOS_nvpart_v1);
44     for (i = 0; i < nb_prom_envs; i++) {
45         end = OpenBIOS_set_var(data, end, prom_envs[i]);
46     }
47 
48     /* End marker */
49     data[end++] = '\0';
50 
51     end = (end + 15) & ~15;
52     /* XXX: OpenBIOS is not able to grow up a partition. Leave some space for
53        new variables. */
54     if (end < min_len) {
55         end = min_len;
56     }
57     OpenBIOS_finish_partition(part_header, end);
58 
59     return end;
60 }
61 
62 /**
63  * Create a "free space" partition
64  */
65 int chrp_nvram_create_free_partition(uint8_t *data, int len)
66 {
67     struct OpenBIOS_nvpart_v1 *part_header;
68 
69     part_header = (struct OpenBIOS_nvpart_v1 *)data;
70     part_header->signature = OPENBIOS_PART_FREE;
71     pstrcpy(part_header->name, sizeof(part_header->name), "free");
72 
73     OpenBIOS_finish_partition(part_header, len);
74 
75     return len;
76 }
77