1+ /* Copyright 2022 The Chromium OS Authors. All rights reserved.
2+ * Use of this source code is governed by a BSD-style license that can be
3+ * found in the LICENSE file.
4+ */
5+
6+ #include "util.h"
7+ #include "hooks.h"
8+
9+ #include "board.h"
10+ #include "gpio.h"
11+
12+ #include "spi.h"
13+ #include "spi_chip.h"
14+ #include "spi_flash.h"
15+
16+ #include "flash_storage.h"
17+
18+
19+
20+
21+
22+
23+ #define CPRINTS (format , args ...) cprints(CC_SYSTEM, format, ## args)
24+ #define CPRINTF (format , args ...) cprintf(CC_SYSTEM, format, ## args)
25+
26+ static struct ec_flash_flags_info current_flags ;
27+ bool flash_storage_dirty ;
28+
29+ bool check_flags_valid_header (void )
30+ {
31+ if (current_flags .magic != FLASH_FLAGS_MAGIC ||
32+ current_flags .length != (sizeof (current_flags ) - 8 ) ||
33+ current_flags .version != FLASH_FLAGS_VERSION ) {
34+ return false;
35+ } else {
36+ return true;
37+ }
38+ }
39+
40+ void flash_storage_load_defaults (void )
41+ {
42+ CPRINTS ("Init flash storage to defaults" );
43+ memset (& current_flags , 0x00 , sizeof (current_flags ));
44+ current_flags .magic = FLASH_FLAGS_MAGIC ;
45+ current_flags .length = (sizeof (current_flags ) - 8 );
46+ current_flags .version = FLASH_FLAGS_VERSION ;
47+ flash_storage_dirty = true;
48+ }
49+
50+ int flash_storage_initialize (void )
51+ {
52+
53+ int rv ;
54+
55+ spi_mux_control (1 );
56+
57+ rv = spi_flash_read ((void * )& current_flags , SPI_FLAGS_REGION , sizeof (current_flags ));
58+ if (rv != EC_SUCCESS )
59+ CPRINTS ("Could not load flash storage" );
60+
61+ spi_mux_control (0 );
62+
63+ /*Check structure is valid*/
64+ if (check_flags_valid_header () == false) {
65+ CPRINTS ("loading flash default flags" );
66+ flash_storage_load_defaults ();
67+ }
68+
69+ return rv ;
70+ }
71+
72+ int flash_storage_update (enum ec_flash_flags_idx idx , uint8_t v )
73+ {
74+ if (idx >= FLASH_FLAGS_MAX )
75+ return EC_ERROR_PARAM1 ;
76+
77+ if (check_flags_valid_header () == false)
78+ flash_storage_initialize ();
79+
80+ if (current_flags .flags [idx ] != v ) {
81+ current_flags .flags [idx ] = v ;
82+ flash_storage_dirty = true;
83+ }
84+ return EC_SUCCESS ;
85+ }
86+
87+ int flash_storage_commit (void )
88+ {
89+ int rv = EC_SUCCESS ;
90+
91+ if (check_flags_valid_header () == false)
92+ flash_storage_initialize ();
93+
94+ if (flash_storage_dirty ) {
95+
96+ spi_mux_control (1 );
97+
98+ rv = spi_flash_erase (SPI_FLAGS_REGION , 0x1000 );
99+
100+ if (rv != EC_SUCCESS ) {
101+ CPRINTS ("SPI fail to erase" );
102+ goto fail ;
103+ }
104+
105+ current_flags .update_number += 1 ;
106+
107+ rv = spi_flash_write (SPI_FLAGS_REGION ,
108+ sizeof (current_flags ),
109+ (void * )& current_flags );
110+
111+ if (rv != EC_SUCCESS ) {
112+ CPRINTS ("SPI fail to write" );
113+ goto fail ;
114+ }
115+
116+ CPRINTS ("%s, update:%d" , __func__ , current_flags .update_number );
117+
118+ spi_mux_control (0 );
119+ flash_storage_dirty = false;
120+ }
121+
122+ return rv ;
123+
124+ fail :
125+ spi_mux_control (0 );
126+ return rv ;
127+ }
128+
129+ int flash_storage_get (enum ec_flash_flags_idx idx )
130+ {
131+ if (idx >= FLASH_FLAGS_MAX )
132+ return -1 ;
133+
134+ if (check_flags_valid_header () == false)
135+ flash_storage_initialize ();
136+
137+ return current_flags .flags [idx ];
138+ }
139+
140+ static int cmd_flash_flags (int argc , char * * argv )
141+ {
142+ int data ;
143+ int i , d ;
144+ char * e ;
145+
146+
147+ if (argc >= 3 ) {
148+
149+ i = strtoi (argv [2 ], & e , 0 );
150+
151+ if (* e )
152+ return EC_ERROR_PARAM2 ;
153+
154+ if (!strcasecmp (argv [1 ], "read" )) {
155+ data = flash_storage_get (i );
156+ CPRINTS ("Flash data:%d" , data );
157+ } else if (argc >= 4 && !strcasecmp (argv [1 ], "write" )) {
158+
159+ d = strtoi (argv [3 ], & e , 0 );
160+ if (* e )
161+ return EC_ERROR_PARAM3 ;
162+ flash_storage_update (i , d );
163+ flash_storage_commit ();
164+ } else {
165+ return EC_ERROR_PARAM3 ;
166+ }
167+ return EC_SUCCESS ;
168+ }
169+
170+ return EC_ERROR_PARAM2 ;
171+ }
172+ DECLARE_CONSOLE_COMMAND (flashflag , cmd_flash_flags ,
173+ "[read/write] i [d]" ,
174+ "read or write bytes from flags structure" );
0 commit comments