33 * Copyright (C) 2022 Intel Corporation <www.intel.com> 
44 * 
55 */ 
6+ #define  DEBUG 
67#include  <common.h> 
78#include  <linux/errno.h> 
89#include  <spi.h> 
1516#define  SSBL_PART_PREFIX "SSBL."
1617#define  RSU_ADDR_MASK 0xFFFFFFFF
1718#define  RSU_ADDR_SHIFT 32
19+ #define  SSBL_PART_PREFIX  "SSBL."
20+ #define  UBOOT_ENV_EXT  ".env"
21+ #define  UBOOT_IMG_EXT  ".img"
22+ #define  UBOOT_ITB_EXT  ".itb"
23+ #define  UBOOT_ENV_PREFIX  "uboot_"
24+ #define  UBOOT_ENV_REDUND_PREFIX  "uboot-redund_"
25+ #define  UBOOT_PREFIX  "u-boot_"
26+ #define  FACTORY_IMG_NAME  "FACTORY_IM"
1827
19- static  int  get_ssbl_slot (struct  socfpga_rsu_s10_spt_slot  * rsu_ssbl_slot )
28+ static  int  get_spl_slot (struct  socfpga_rsu_s10_spt  * rsu_spt ,
29+ size_t  rsu_spt_size , int  * crt_spt_index )
2030{
21- struct  socfpga_rsu_s10_spt  rsu_spt  =  {0 };
2231u32  rsu_spt0_offset  =  0 , rsu_spt1_offset  =  0 ;
23- u32  spt_offset [4 ];
24- struct  rsu_status_info  rsu_status ;
25- int  crt_spt_index  =  - EINVAL ;
26- char  * result ;
32+ u32  spt_offset [4 ] =  {0 };
33+ struct  rsu_status_info  rsu_status  =  {0 };
2734struct  spi_flash  * flash ;
2835int  i ;
2936
30- rsu_ssbl_slot -> offset [0 ] =  - EINVAL ;
31- 
3237/* get rsu status */ 
3338if  (mbox_rsu_status ((u32  * )& rsu_status , sizeof (rsu_status ) / 4 )) {
3439puts ("RSU: Error - mbox_rsu_status failed!\n" );
@@ -55,23 +60,23 @@ static int get_ssbl_slot(struct socfpga_rsu_s10_spt_slot *rsu_ssbl_slot)
5560}
5661
5762/* read spt0 */ 
58- if  (spi_flash_read (flash , rsu_spt0_offset , sizeof ( rsu_spt ),  & rsu_spt )) {
63+ if  (spi_flash_read (flash , rsu_spt0_offset , rsu_spt_size ,  rsu_spt )) {
5964puts ("RSU: Error - spi_flash_read failed!\n" );
6065return  - EINVAL ;
6166}
6267
6368/* if spt0 does not have the correct magic number */ 
64- if  (rsu_spt . magic_number  !=  RSU_S10_SPT_MAGIC_NUMBER ) {
69+ if  (rsu_spt -> magic_number  !=  RSU_S10_SPT_MAGIC_NUMBER ) {
6570/* read spt1 */ 
66- if  (spi_flash_read (flash , rsu_spt1_offset , sizeof ( rsu_spt ),  & rsu_spt )) {
71+ if  (spi_flash_read (flash , rsu_spt1_offset , rsu_spt_size ,  rsu_spt )) {
6772printf ("RSU: Error - spi_flash_read failed!\n" );
6873return  - EINVAL ;
6974}
7075
7176/* bail out if spt1 does not have the correct magic number */ 
72- if  (rsu_spt . magic_number  !=  RSU_S10_SPT_MAGIC_NUMBER ) {
77+ if  (rsu_spt -> magic_number  !=  RSU_S10_SPT_MAGIC_NUMBER ) {
7378printf ("RSU: Error: spt table magic number not match 0x%08x!\n" ,
74-  rsu_spt . magic_number );
79+  rsu_spt -> magic_number );
7580return  - EINVAL ;
7681}
7782}
@@ -83,24 +88,38 @@ static int get_ssbl_slot(struct socfpga_rsu_s10_spt_slot *rsu_ssbl_slot)
8388debug ("RSU error details: 0x%08x\n" , rsu_status .error_details );
8489
8590/* display partitions */ 
86- for  (i  =  0 ; i  <  rsu_spt . entries ; i ++ ) {
91+ for  (i  =  0 ; i  <  rsu_spt -> entries ; i ++ ) {
8792debug ("RSU: Partition '%s' start=0x%08x length=0x%08x\n" ,
88-  rsu_spt . spt_slot [i ].name , rsu_spt . spt_slot [i ].offset [0 ],
89-  rsu_spt . spt_slot [i ].length );
93+  rsu_spt -> spt_slot [i ].name , rsu_spt -> spt_slot [i ].offset [0 ],
94+  rsu_spt -> spt_slot [i ].length );
9095}
9196
9297/* locate the SPT entry for currently loaded image */ 
93- for  (i  =  0 ; i  <  rsu_spt . entries ; i ++ ) {
98+ for  (i  =  0 ; i  <  rsu_spt -> entries ; i ++ ) {
9499if  (((rsu_status .current_image  &  RSU_ADDR_MASK ) == 
95- rsu_spt . spt_slot [i ].offset [0 ]) && 
100+ rsu_spt -> spt_slot [i ].offset [0 ]) && 
96101 ((rsu_status .current_image  >> RSU_ADDR_SHIFT ) == 
97- rsu_spt . spt_slot [i ].offset [1 ])) {
98- crt_spt_index  =  i ;
99- break ;
102+ rsu_spt -> spt_slot [i ].offset [1 ])) {
103+ * crt_spt_index  =  i ;
104+ return   0 ;
100105}
101106}
102107
103- if  (crt_spt_index  ==  - EINVAL ) {
108+ puts ("RSU: Error - could not locate SPL partition in the SPT table!\n" );
109+ return  - EINVAL ;
110+ }
111+ 
112+ static  int  get_ssbl_slot (struct  socfpga_rsu_s10_spt_slot  * rsu_ssbl_slot )
113+ {
114+ struct  socfpga_rsu_s10_spt  rsu_spt  =  {0 };
115+ int  crt_spt_index  =  - EINVAL ;
116+ char  * result ;
117+ int  i , ret ;
118+ 
119+ rsu_ssbl_slot -> offset [0 ] =  - EINVAL ;
120+ 
121+ ret  =  get_spl_slot (& rsu_spt , sizeof (rsu_spt ), & crt_spt_index );
122+ if  (ret ) {
104123puts ("RSU: Error - could not locate partition in the SPT table!\n" );
105124return  - EINVAL ;
106125}
@@ -121,7 +140,8 @@ static int get_ssbl_slot(struct socfpga_rsu_s10_spt_slot *rsu_ssbl_slot)
121140
122141/* compare SPL's spt name after the prefix */ 
123142if  (!strncmp (result , rsu_spt .spt_slot [crt_spt_index ].name ,
124-  MAX_PART_NAME_LENGTH  -  strlen (SSBL_PART_PREFIX ))) {
143+  MAX_PART_NAME_LENGTH  -  strlen (SSBL_PART_PREFIX )) || 
144+ !strncmp (result , FACTORY_IMG_NAME , strlen (FACTORY_IMG_NAME ))) {
125145printf ("RSU: found SSBL partition %s at address 0x%08x.\n" ,
126146 result , (int )rsu_spt .spt_slot [i ].offset [0 ]);
127147memcpy (rsu_ssbl_slot , & rsu_spt .spt_slot [i ],
@@ -139,7 +159,100 @@ static int get_ssbl_slot(struct socfpga_rsu_s10_spt_slot *rsu_ssbl_slot)
139159return  - EINVAL ;
140160}
141161
142- u32  rsu_spl_ssbl_address (void )
162+ int  rsu_spl_mmc_filename (char  * filename , int  max_size )
163+ {
164+ struct  socfpga_rsu_s10_spt  rsu_spt  =  {0 };
165+ int  crt_spt_index  =  - EINVAL ;
166+ int  ret , len ;
167+ 
168+ if  (!filename ) {
169+ printf ("RSU: filename is NULL!\n" );
170+ return  - ENOENT ;
171+ }
172+ 
173+ if  ((strlen (UBOOT_PREFIX ) +  MAX_PART_NAME_LENGTH  +  strlen (UBOOT_ITB_EXT ))
174+  >  max_size )
175+ return  - ENAMETOOLONG ;
176+ 
177+ ret  =  get_spl_slot (& rsu_spt , sizeof (rsu_spt ), & crt_spt_index );
178+ if  (ret ) {
179+ if  (ret  ==  - EOPNOTSUPP ) {
180+ puts ("RSU: Error - mbox_rsu_status failed! Check for RSU image.\n" );
181+ return  - EOPNOTSUPP ;
182+ }
183+ 
184+ /* should throw error if cannot find u-boot proper(SSBL) in MMC */ 
185+ panic ("ERROR: could not find u-boot proper(SSBL): SSBL.%s!" ,
186+  rsu_spt .spt_slot [crt_spt_index ].name );
187+ }
188+ 
189+ /* add 1 to the length to copy for NULL terminated string */ 
190+ strlcat (filename , UBOOT_PREFIX , strlen (UBOOT_PREFIX ) +  1 );
191+ strlcat (filename  +  strlen (UBOOT_PREFIX ),
192+ rsu_spt .spt_slot [crt_spt_index ].name ,
193+ strlen (rsu_spt .spt_slot [crt_spt_index ].name ) +  1 );
194+ len  =  strlen (UBOOT_PREFIX ) +  strlen (rsu_spt .spt_slot [crt_spt_index ].name );
195+ #if  IS_ENABLED (CONFIG_SPL_LOAD_FIT )
196+ strlcat (filename  +  len ,
197+ UBOOT_ITB_EXT , strlen (UBOOT_ITB_EXT ) +  1 );
198+ #else 
199+ strlcat (filename  +  len ,
200+ UBOOT_IMG_EXT , strlen (UBOOT_IMG_EXT ) +  1 );
201+ #endif 
202+ printf ("%s, filename: %s\n" , __func__ , filename );
203+ return  0 ;
204+ }
205+ 
206+ int  rsu_spl_mmc_env_name (char  * filename , int  max_size , bool  redund )
207+ {
208+ struct  socfpga_rsu_s10_spt  rsu_spt  =  {0 };
209+ int  crt_spt_index  =  - EINVAL ;
210+ int  ret , len ;
211+ 
212+ if  (!filename ) {
213+ printf ("RSU: filename is NULL!\n" );
214+ return  - ENOENT ;
215+ }
216+ 
217+ if  ((strlen (UBOOT_ENV_REDUND_PREFIX ) +  strlen (UBOOT_ENV_PREFIX ) + 
218+  MAX_PART_NAME_LENGTH  +  strlen (UBOOT_ENV_EXT )) >  max_size )
219+ return  - ENAMETOOLONG ;
220+ 
221+ ret  =  get_spl_slot (& rsu_spt , sizeof (rsu_spt ), & crt_spt_index );
222+ if  (ret ) {
223+ if  (ret  ==  - EOPNOTSUPP ) {
224+ puts ("RSU: Error - mbox_rsu_status failed! Check for RSU image.\n" );
225+ return  - EOPNOTSUPP ;
226+ }
227+ 
228+ /* should throw error if cannot find u-boot proper(SSBL) in MMC */ 
229+ printf ("ERROR: could not find u-boot.env!" );
230+ return  ret ;
231+ }
232+ 
233+ if  (redund ) {
234+ strlcat (filename , UBOOT_ENV_REDUND_PREFIX ,
235+ strlen (UBOOT_ENV_REDUND_PREFIX ) +  1 );
236+ strlcat (filename  +  strlen (UBOOT_ENV_REDUND_PREFIX ),
237+ rsu_spt .spt_slot [crt_spt_index ].name ,
238+ strlen (rsu_spt .spt_slot [crt_spt_index ].name ) +  1 );
239+ len  =  strlen (UBOOT_ENV_REDUND_PREFIX ) + 
240+  strlen (rsu_spt .spt_slot [crt_spt_index ].name );
241+ } else  {
242+ strlcat (filename , UBOOT_ENV_PREFIX , strlen (UBOOT_ENV_PREFIX ) +  1 );
243+ strlcat (filename  +  strlen (UBOOT_ENV_PREFIX ),
244+ rsu_spt .spt_slot [crt_spt_index ].name ,
245+ strlen (rsu_spt .spt_slot [crt_spt_index ].name ) +  1 );
246+ len  =  strlen (UBOOT_ENV_PREFIX ) + 
247+  strlen (rsu_spt .spt_slot [crt_spt_index ].name );
248+ }
249+ strlcat (filename  +  len , UBOOT_ENV_EXT , strlen (UBOOT_ENV_EXT ) +  1 );
250+ 
251+ printf ("%s, filename: %s\n" , __func__ , filename );
252+ return  0 ;
253+ }
254+ 
255+ u32  rsu_spl_ssbl_address (bool  is_qspi_imge_check )
143256{
144257int  ret ;
145258struct  socfpga_rsu_s10_spt_slot  rsu_ssbl_slot  =  {0 };
@@ -152,33 +265,53 @@ u32 rsu_spl_ssbl_address(void)
152265}
153266
154267/* should throw error if cannot find u-boot proper(SSBL) address */ 
155- panic ("ERROR: could not find u-boot proper(SSBL) address!" );
268+ if  (is_qspi_imge_check ) {
269+ panic ("ERROR: could not find u-boot proper(SSBL) address!" );
270+ } else  {
271+ printf ("ERROR: could not find u-boot env address!" );
272+ return  ret ;
273+ }
156274}
157275
158- printf ("RSU: Success found SSBL at offset: %08x.\n" , rsu_ssbl_slot .offset [0 ]);
276+ printf ("RSU: Success found SSBL at offset: %08x.\n" ,
277+  rsu_ssbl_slot .offset [0 ]);
159278return  rsu_ssbl_slot .offset [0 ];
160279}
161280
162- u32  rsu_spl_ssbl_size (void )
281+ u32  rsu_spl_ssbl_size (bool   is_qspi_imge_check )
163282{
283+ int  ret ;
164284struct  socfpga_rsu_s10_spt_slot  rsu_ssbl_slot  =  {0 };
165285
166286/* check for valid u-boot proper(SSBL) address for the size */ 
167- if  (get_ssbl_slot (& rsu_ssbl_slot ) ==  - EOPNOTSUPP ) {
168- printf ("ERROR: Invalid address, could not retrieve SSBL size!" );
169- return  0 ;
287+ ret  =  get_ssbl_slot (& rsu_ssbl_slot );
288+ if  (ret ) {
289+ if  (ret  ==  - EOPNOTSUPP ) {
290+ printf ("ERROR: Invalid address, could not retrieve SSBL size!" );
291+ return  0 ;
292+ }
293+ 
294+ /* should throw error if cannot find u-boot proper(SSBL) address */ 
295+ if  (is_qspi_imge_check ) {
296+ panic ("ERROR: could not find u-boot proper(SSBL) address!" );
297+ } else  {
298+ printf ("ERROR: could not find u-boot env address!" );
299+ return  ret ;
300+ }
170301}
171302
172303if  (!rsu_ssbl_slot .length ) {
173304/* throw error if cannot find u-boot proper(SSBL) size */ 
174- panic ("ERROR: could not retrieve u-boot proper(SSBL) size!" );
305+ printf ("ERROR: could not retrieve u-boot proper(SSBL) size!" );
306+ return  ret ;
175307}
176308
177- printf ("RSU: Success found SSBL with length: %08x.\n" , rsu_ssbl_slot .length );
309+ printf ("RSU: Success found SSBL with length: %08x.\n" ,
310+  rsu_ssbl_slot .length );
178311return  rsu_ssbl_slot .length ;
179312}
180313
181314unsigned int   spl_spi_get_uboot_offs (struct  spi_flash  * flash )
182315{
183- return  rsu_spl_ssbl_address ();
316+ return  rsu_spl_ssbl_address (true );
184317}
0 commit comments