88use log:: { debug, error, info, trace, warn} ;
99
1010use core:: future:: Future ;
11+ use core:: mem:: MaybeUninit ;
12+
13+ use static_cell:: StaticCell ;
1114
1215use pldm_file:: PLDM_TYPE_FILE_TRANSFER ;
1316use pldm_platform:: proto:: PdrRecord ;
@@ -44,6 +47,13 @@ pub trait PldmTimeout: Future + Sized {
4447
4548impl < F : Future > PldmTimeout for F { }
4649
50+ // Limited by MCTP message size, must be power of two
51+ const PART_SIZE : usize = 4096 ;
52+ // sram2 is not zeroed at boot, so need MaybeUninit.
53+ #[ link_section = ".sram2_uninit" ]
54+ static mut PART_BUF : MaybeUninit < StaticCell < [ u8 ; PART_SIZE + 18 ] > > =
55+ MaybeUninit :: uninit ( ) ;
56+
4757#[ embassy_executor:: task]
4858pub ( crate ) async fn pldm_file_task (
4959 router : & ' static Router < ' static > ,
@@ -52,8 +62,12 @@ pub(crate) async fn pldm_file_task(
5262) -> ! {
5363 info ! ( "PLDM file task started" ) ;
5464
55- let mut host = None ;
65+ // Safety: will only be written once, no shared references.
66+ #[ allow( static_mut_refs) ]
67+ let part_buf = unsafe { PART_BUF . write ( StaticCell :: new ( ) ) } ;
68+ let part_buf = part_buf. init_with ( || [ 0u8 ; _] ) ;
5669
70+ let mut host = None ;
5771 loop {
5872 let target = match host. take ( ) {
5973 Some ( t) => t,
@@ -63,7 +77,8 @@ pub(crate) async fn pldm_file_task(
6377 info ! ( "Running PLDM file transfer from {target}" ) ;
6478
6579 let run = async {
66- if let Err ( e) = pldm_run_file ( target, router, hash) . await {
80+ if let Err ( e) = pldm_run_file ( target, router, hash, part_buf) . await
81+ {
6782 warn ! ( "Error running file transfer: {e}" ) ;
6883 }
6984 } ;
@@ -143,6 +158,7 @@ async fn pldm_run_file(
143158 eid : Eid ,
144159 router : & ' static Router < ' static > ,
145160 hash : & ' static SharedHash ,
161+ part_buf : & mut [ u8 ] ,
146162) -> Result < ( ) , PldmError > {
147163 use pldm_file:: client:: * ;
148164 use pldm_file:: proto:: * ;
@@ -242,7 +258,10 @@ async fn pldm_run_file(
242258 // NegotiateTransferParameters
243259 let req_types = [ pldm_file:: PLDM_TYPE_FILE_TRANSFER ] ;
244260 let ( size, neg_types) = ctrq:: negotiate_transfer_parameters (
245- comm, & req_types, & mut buf, 1024 ,
261+ comm,
262+ & req_types,
263+ & mut buf,
264+ PART_SIZE as u16 ,
246265 )
247266 . await
248267 . inspect_err ( |e| warn ! ( "Error from Negotiate: {e}" ) ) ?;
@@ -275,11 +294,18 @@ async fn pldm_run_file(
275294 None ,
276295 ) ;
277296 let mut count = 0 ;
278- df_read_with ( comm, fd, 0 , filedesc. file_max_size as usize , |b| {
279- count += b. len ( ) ;
280- hash. update_blocking ( & mut hash_ctx, b) ;
281- Ok ( ( ) )
282- } )
297+ df_read_with (
298+ comm,
299+ fd,
300+ 0 ,
301+ filedesc. file_max_size as usize ,
302+ part_buf,
303+ |b| {
304+ count += b. len ( ) ;
305+ hash. update_blocking ( & mut hash_ctx, b) ;
306+ Ok ( ( ) )
307+ } ,
308+ )
283309 . with_timeout ( READ_TIMEOUT )
284310 . await ?
285311 . inspect_err ( |e| warn ! ( "df_read failed {e}" ) ) ?;
0 commit comments