xref: /openbmc/qemu/hw/nvram/chrp_nvram.c (revision 59a3a1c0)
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/nvram/chrp_nvram.h"
25 #include "sysemu/sysemu.h"
26 
27 static int chrp_nvram_set_var(uint8_t *nvram, int addr, const char *str)
28 {
29     int len;
30 
31     len = strlen(str) + 1;
32     memcpy(&nvram[addr], str, len);
33 
34     return addr + len;
35 }
36 
37 /**
38  * Create a "system partition", used for the Open Firmware
39  * environment variables.
40  */
41 int chrp_nvram_create_system_partition(uint8_t *data, int min_len)
42 {
43     ChrpNvramPartHdr *part_header;
44     unsigned int i;
45     int end;
46 
47     part_header = (ChrpNvramPartHdr *)data;
48     part_header->signature = CHRP_NVPART_SYSTEM;
49     pstrcpy(part_header->name, sizeof(part_header->name), "system");
50 
51     end = sizeof(ChrpNvramPartHdr);
52     for (i = 0; i < nb_prom_envs; i++) {
53         end = chrp_nvram_set_var(data, end, prom_envs[i]);
54     }
55 
56     /* End marker */
57     data[end++] = '\0';
58 
59     end = (end + 15) & ~15;
60     /* XXX: OpenBIOS is not able to grow up a partition. Leave some space for
61        new variables. */
62     if (end < min_len) {
63         end = min_len;
64     }
65     chrp_nvram_finish_partition(part_header, end);
66 
67     return end;
68 }
69 
70 /**
71  * Create a "free space" partition
72  */
73 int chrp_nvram_create_free_partition(uint8_t *data, int len)
74 {
75     ChrpNvramPartHdr *part_header;
76 
77     part_header = (ChrpNvramPartHdr *)data;
78     part_header->signature = CHRP_NVPART_FREE;
79     pstrcpy(part_header->name, sizeof(part_header->name), "free");
80 
81     chrp_nvram_finish_partition(part_header, len);
82 
83     return len;
84 }
85