|
| 1 | +// Licensed under the Apache-2.0 license |
| 2 | +use crate::codec::CommonCodec; |
| 3 | +use crate::context::SpdmContext; |
| 4 | +use crate::protocol::*; |
| 5 | +use bitfield::bitfield; |
| 6 | +use zerocopy::{FromBytes, Immutable, IntoBytes}; |
| 7 | + |
| 8 | +/// The maximum number of chunks that can be transferred for a large response. |
| 9 | +/// This is determined by the size of the chunk sequence number field (u16) in the |
| 10 | +/// `ChunkGetReq` and `ChunkResponseFixed` messages. |
| 11 | +pub const MAX_NUM_CHUNKS: u16 = u16::MAX; |
| 12 | + |
| 13 | +pub mod request; |
| 14 | +pub mod response; |
| 15 | + |
| 16 | +#[derive(FromBytes, IntoBytes, Immutable)] |
| 17 | +#[repr(C)] |
| 18 | +pub(crate) struct ChunkGet { |
| 19 | + /// Reserved. |
| 20 | + param1: u8, |
| 21 | + |
| 22 | + /// Shall contain a handle. This field shall be the same value as given in the |
| 23 | + /// `Handle` field of the `ERROR` message of `ErrorCode=LargeResponse`. |
| 24 | + /// |
| 25 | + /// # Note |
| 26 | + /// |
| 27 | + /// The fields name in spec is `Param2`. |
| 28 | + handle: u8, |
| 29 | + |
| 30 | + /// Shall indicate the desired chunk sequence number of the Large SPDM Response |
| 31 | + /// to retrieve. |
| 32 | + chunk_seq_no: u16, |
| 33 | +} |
| 34 | +impl CommonCodec for ChunkGet {} |
| 35 | + |
| 36 | +#[derive(FromBytes, IntoBytes, Immutable)] |
| 37 | +#[repr(C, packed)] |
| 38 | +/// The fixed fields of the `CHUNK_RESPONSE` message. The actual response payload |
| 39 | +/// follows these fixed fields in the message. |
| 40 | +/// |
| 41 | +/// - `LargeResponseSize`: Field is only present in the first chunk (i.e., when `ChunkSeqNo` is 0). |
| 42 | +/// - `SPDMchunk`: The chunk of the large response message. The size of this field is determined by the |
| 43 | +struct ChunkResponseFixed { |
| 44 | + /// # Note |
| 45 | + /// |
| 46 | + /// The fields name in spec is `Param1`. |
| 47 | + chunk_sender_attr: ChunkSenderAttr, |
| 48 | + |
| 49 | + /// # Note |
| 50 | + /// |
| 51 | + /// The fields name in spec is `Param2`. |
| 52 | + handle: u8, |
| 53 | + |
| 54 | + chunk_seq_no: u16, |
| 55 | + reserved: u16, |
| 56 | + chunk_size: u32, |
| 57 | +} |
| 58 | +impl CommonCodec for ChunkResponseFixed {} |
| 59 | + |
| 60 | +bitfield! { |
| 61 | + #[derive(FromBytes, IntoBytes, Immutable)] |
| 62 | + #[repr(C)] |
| 63 | + struct ChunkSenderAttr(u8); |
| 64 | + impl Debug; |
| 65 | + u8; |
| 66 | + /// If set, the chunk indicated by `ChunkSeqNo` shall represent the last chunk |
| 67 | + /// of the large SPDM message. |
| 68 | + pub last_chunk, set_last_chunk: 0, 0; |
| 69 | + reserved, _: 7, 1; |
| 70 | +} |
| 71 | + |
| 72 | +bitfield! { |
| 73 | + #[derive(FromBytes, IntoBytes, Immutable)] |
| 74 | + #[repr(C)] |
| 75 | + struct ChunkReceiverAttr(u8); |
| 76 | + impl Debug; |
| 77 | + u8; |
| 78 | + /// If set, the receiver of a large SPDM request message detected an error in |
| 79 | + /// the Request before the last chunk was received. If set, the sender of the |
| 80 | + /// large SPDM request message shall terminate the transfer of any remaining chunks. |
| 81 | + /// After addressing the issue, the sender of the failed large SPDM request |
| 82 | + /// message can transfer the fixed large SPDM request message as a new transfer. |
| 83 | + pub early_error_detected, set_early_error_detected: 0, 0; |
| 84 | + reserved, _: 7, 1; |
| 85 | +} |
| 86 | + |
| 87 | +#[derive(FromBytes, IntoBytes, Immutable)] |
| 88 | +#[repr(C)] |
| 89 | +struct LargeResponseSize(u32); |
| 90 | +impl CommonCodec for LargeResponseSize {} |
| 91 | + |
| 92 | +#[derive(FromBytes, IntoBytes, Immutable)] |
| 93 | +#[repr(C)] |
| 94 | +/// The fixed size components of the `CHUNK_SEND` request. |
| 95 | +/// When sent, this struct is followed by |
| 96 | +/// - `LargeMessageSize` field (only present in the first chunk, i.e., when `ChunkSeqNo` is 0) |
| 97 | +pub(crate) struct ChunkSendFixed { |
| 98 | + param1: ChunkSenderAttr, |
| 99 | + |
| 100 | + /// # Note |
| 101 | + /// |
| 102 | + /// The fields name in spec is `Param2`. |
| 103 | + handle: u8, |
| 104 | + |
| 105 | + /// Shall identify the chunk sequence number associated with SPDMchunk . |
| 106 | + chunk_seq_no: u32, |
| 107 | + |
| 108 | + chunk_size: u32, |
| 109 | +} |
| 110 | + |
| 111 | +impl ChunkSendFixed { |
| 112 | + pub fn new(handle: u8, chunk_seq_no: u32, chunk_size: u32, last_chunk: bool) -> Self { |
| 113 | + let mut sender_attr = ChunkSenderAttr(0); |
| 114 | + sender_attr.set_last_chunk(last_chunk as u8); |
| 115 | + Self { |
| 116 | + param1: sender_attr, |
| 117 | + handle, |
| 118 | + chunk_seq_no, |
| 119 | + chunk_size, |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#[derive(FromBytes, IntoBytes, Immutable)] |
| 125 | +#[repr(C)] |
| 126 | +/// Size of the large SPDM message being transferred. This field shall only be present when ChunkSeqNo is zero and shall have a non-zero value. Shall be greater than the DataTransferSize of the receiving SPDM endpoint. |
| 127 | +struct LargeMessageSize(u32); |
| 128 | +impl CommonCodec for LargeMessageSize {} |
| 129 | + |
| 130 | +#[derive(FromBytes, IntoBytes, Immutable)] |
| 131 | +#[repr(C)] |
| 132 | +/// # Note |
| 133 | +/// This struct may be followed by the variable length field `ResponseToLargeRequest`. |
| 134 | +/// `ResponseToLargeRequest` shall be present on the last chunk (that is, when LastChunk is set), |
| 135 | +/// or when the `EarlyErrorDetected` bit in Param1 is set. |
| 136 | +/// |
| 137 | +/// This field shall contain the response to the large SPDM request message. |
| 138 | +/// When the `EarlyErrorDetected` bit in Param1 is set, this field shall contain an ERROR message. |
| 139 | +pub(crate) struct ChunkSendAck { |
| 140 | + param1: ChunkReceiverAttr, |
| 141 | + /// # Note |
| 142 | + /// |
| 143 | + /// The fields name in spec is `Param2`. |
| 144 | + handle: u8, |
| 145 | + chunk_seq_no: u32, |
| 146 | +} |
| 147 | + |
| 148 | +/// Maximal size of the large response that can be transferred in chunks. |
| 149 | +/// If a large response exceeds this size, it cannot be transferred in chunks and |
| 150 | +/// the requester should not send a CHUNK_GET request for it. |
| 151 | +pub(crate) fn max_chunked_resp_size(ctx: &SpdmContext) -> usize { |
| 152 | + let min_data_transfer_size = ctx.min_data_transfer_size(); |
| 153 | + let fixed_chunk_resp_size = size_of::<SpdmMsgHdr>() + size_of::<ChunkResponseFixed>(); |
| 154 | + |
| 155 | + // compute max possible response size that can be transferred in chunks is less than the large response size |
| 156 | + (min_data_transfer_size).saturating_sub(fixed_chunk_resp_size) * MAX_NUM_CHUNKS as usize |
| 157 | + - size_of::<u32>() |
| 158 | +} |
| 159 | + |
| 160 | +// Computes the chunk size based on the context and the chunk sequence number |
| 161 | +// Returns the chunk size and a boolean indicating if this is the last chunk |
| 162 | +fn compute_chunk_size(ctx: &SpdmContext, chunk_seq_num: u16) -> (usize, bool) { |
| 163 | + let extra_field_size = if chunk_seq_num == 0 { |
| 164 | + size_of::<LargeResponseSize>() |
| 165 | + } else { |
| 166 | + 0 |
| 167 | + }; |
| 168 | + let chunk_size = ctx.min_data_transfer_size().saturating_sub( |
| 169 | + size_of::<SpdmMsgHdr>() + size_of::<ChunkResponseFixed>() + extra_field_size, |
| 170 | + ); |
| 171 | + |
| 172 | + let (is_last_chunk, remaining_len) = ctx.large_resp_context.last_chunk(chunk_size); |
| 173 | + |
| 174 | + if is_last_chunk { |
| 175 | + (remaining_len, true) |
| 176 | + } else { |
| 177 | + (chunk_size, false) |
| 178 | + } |
| 179 | +} |
0 commit comments