hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/** @file
  Platform flash device access library for Marvell Armada 7k8k Platforms
 
  Copyright (c) 2018 Marvell International Ltd.<BR>
 
  SPDX-License-Identifier: BSD-2-Clause-Patent
 
**/
 
#include <PiDxe.h>
 
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/DxeServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/PlatformFlashAccessLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
 
#include <Protocol/Spi.h>
#include <Protocol/SpiFlash.h>
 
#define MAIN_HDR_MAGIC        0xB105B002
 
STATIC MARVELL_SPI_FLASH_PROTOCOL *SpiFlashProtocol;
STATIC MARVELL_SPI_MASTER_PROTOCOL *SpiMasterProtocol;
 
typedef struct {              // Bytes
  UINT32  Magic;              //  0-3
  UINT32  PrologSize;         //  4-7
  UINT32  PrologChecksum;     //  8-11
  UINT32  BootImageSize;      // 12-15
  UINT32  BootImageChecksum;  // 16-19
  UINT32  Reserved0;          // 20-23
  UINT32  LoadAddr;           // 24-27
  UINT32  ExecAddr;           // 28-31
  UINT8   UartConfig;         //  32
  UINT8   Baudrate;           //  33
  UINT8   ExtCount;           //  34
  UINT8   AuxFlags;           //  35
  UINT32  IoArg0;             // 36-39
  UINT32  IoArg1;             // 40-43
  UINT32  IoArg2;             // 43-47
  UINT32  IoArg3;             // 48-51
  UINT32  Reserved1;          // 52-55
  UINT32  Reserved2;          // 56-59
  UINT32  Reserved3;          // 60-63
} MV_FIRMWARE_IMAGE_HEADER;
 
STATIC
EFI_STATUS
SpiFlashProbe (
  IN SPI_DEVICE    *SpiFlash
  )
{
  EFI_STATUS       Status;
 
  // Read SPI flash ID
  Status = SpiFlashProtocol->ReadId (SpiFlash, FALSE);
  if (EFI_ERROR (Status)) {
    return Status;
  }
 
  Status = SpiFlashProtocol->Init (SpiFlashProtocol, SpiFlash);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: Cannot initialize flash device\n", __FUNCTION__));
    return Status;
  }
 
  return EFI_SUCCESS;
}
 
STATIC
EFI_STATUS
CheckImageHeader (
  IN OUT VOID     *ImageBuffer
  )
{
  MV_FIRMWARE_IMAGE_HEADER *Header;
  UINT32 HeaderLength, Checksum, ChecksumBackup;
 
  Header = (MV_FIRMWARE_IMAGE_HEADER *)ImageBuffer;
  HeaderLength = Header->PrologSize;
  ChecksumBackup = Header->PrologChecksum;
 
  // Compare magic number
  if (Header->Magic != MAIN_HDR_MAGIC) {
    DEBUG ((DEBUG_ERROR,
      "%a: Bad Image magic 0x%08x != 0x%08x\n",
      __FUNCTION__,
      Header->Magic,
      MAIN_HDR_MAGIC));
    return EFI_VOLUME_CORRUPTED;
  }
 
  // The checksum field is discarded from calculation
  Header->PrologChecksum = 0;
 
  Checksum = CalculateSum32 ((UINT32 *)Header, HeaderLength);
  if (Checksum != ChecksumBackup) {
    DEBUG ((DEBUG_ERROR,
      "%a: Bad Image checksum. 0x%x != 0x%x\n",
      __FUNCTION__,
      Checksum,
      ChecksumBackup));
    return EFI_VOLUME_CORRUPTED;
  }
 
  // Restore checksum backup
  Header->PrologChecksum = ChecksumBackup;
 
  return EFI_SUCCESS;
}
 
/**
  Perform flash write operation with progress indicator.  The start and end
  completion percentage values are passed into this function.  If the requested
  flash write operation is broken up, then completion percentage between the
  start and end values may be passed to the provided Progress function.  The
  caller of this function is required to call the Progress function for the
  start and end completion percentage values.  This allows the Progress,
  StartPercentage, and EndPercentage parameters to be ignored if the requested
  flash write operation can not be broken up
 
  @param[in] FirmwareType      The type of firmware.
  @param[in] FlashAddress      The address of flash device to be accessed.
  @param[in] FlashAddressType  The type of flash device address.
  @param[in] Buffer            The pointer to the data buffer.
  @param[in] Length            The length of data buffer in bytes.
  @param[in] Progress          A function used report the progress of the
                               firmware update.  This is an optional parameter
                               that may be NULL.
  @param[in] StartPercentage   The start completion percentage value that may
                               be used to report progress during the flash
                               write operation.
  @param[in] EndPercentage     The end completion percentage value that may
                               be used to report progress during the flash
                               write operation.
 
  @retval EFI_SUCCESS           The operation returns successfully.
  @retval EFI_WRITE_PROTECTED   The flash device is read only.
  @retval EFI_UNSUPPORTED       The flash device access is unsupported.
  @retval EFI_INVALID_PARAMETER The input parameter is not valid.
**/
EFI_STATUS
EFIAPI
PerformFlashWriteWithProgress (
  IN PLATFORM_FIRMWARE_TYPE                         FirmwareType,
  IN EFI_PHYSICAL_ADDRESS                           FlashAddress,
  IN FLASH_ADDRESS_TYPE                             FlashAddressType,
  IN VOID                                           *Buffer,
  IN UINTN                                          Length,
  IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS  Progress,        OPTIONAL
  IN UINTN                                          StartPercentage,
  IN UINTN                                          EndPercentage
  )
{
  EFI_STATUS              Status;
  VOID                    *ImageBuffer;
  SPI_DEVICE              *SpiFlash = NULL;
  BOOLEAN                 BufferAligned = TRUE;
 
  // Verify Firmware data
  if (FlashAddressType != FlashAddressTypeAbsoluteAddress) {
    DEBUG ((DEBUG_ERROR,
      "%a: only FlashAddressTypeAbsoluteAddress supported\n",
      __FUNCTION__));
 
    return EFI_INVALID_PARAMETER;
  }
 
  if (FirmwareType != PlatformFirmwareTypeSystemFirmware) {
    DEBUG ((DEBUG_ERROR,
      "%a: only PlatformFirmwareTypeSystemFirmware supported\n",
      __FUNCTION__));
 
    return EFI_INVALID_PARAMETER;
  }
 
  // Locate SPI protocols
  Status = gBS->LocateProtocol (&gMarvellSpiFlashProtocolGuid,
                  NULL,
                  (VOID **)&SpiFlashProtocol);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR,
      "%a: Cannot locate SpiFlash protocol\n",
      __FUNCTION__));
    return Status;
  }
 
  Status = gBS->LocateProtocol (&gMarvellSpiMasterProtocolGuid,
                  NULL,
                  (VOID **)&SpiMasterProtocol);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR,
      "%a: Cannot locate SpiMaster protocol\n",
      __FUNCTION__));
    return Status;
  }
 
  //
  // Counting checksum in the header verification requires
  // the buffer address alignment.
  // It is not guaranteed by the generic capsule handling code,
  // so use an auxiliary buffer in such case.
  //
  if (((UINTN) Buffer & 0x3) != 0) {
    ImageBuffer = AllocateCopyPool (Length, Buffer);
    if (ImageBuffer == NULL) {
      return EFI_OUT_OF_RESOURCES;
    }
    BufferAligned = FALSE;
  } else {
    ImageBuffer = Buffer;
  }
 
  // Check image checksum and magic
  Status = CheckImageHeader (ImageBuffer);
  if (EFI_ERROR (Status)) {
    goto HeaderError;
  }
 
  // Setup and probe SPI flash
  SpiFlash = SpiMasterProtocol->SetupDevice (SpiMasterProtocol,
                                  SpiFlash,
                                  PcdGet32 (PcdSpiFlashCs),
                                  PcdGet32 (PcdSpiFlashMode));
  if (SpiFlash == NULL) {
    DEBUG ((DEBUG_ERROR, "%a: Cannot allocate SPI device!\n", __FUNCTION__));
    Status = EFI_DEVICE_ERROR;
    goto HeaderError;
  }
 
  Status = SpiFlashProbe (SpiFlash);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR,
      "%a: Error while performing SPI flash probe\n",
      __FUNCTION__));
    goto FlashProbeError;
  }
 
  // Update firmware image in flash
  if (Progress != NULL) {
    Status = SpiFlashProtocol->UpdateWithProgress (SpiFlash,
                                 FlashAddress,
                                 Length,
                                 (UINT8 *)ImageBuffer,
                                 Progress,
                                 StartPercentage,
                                 EndPercentage);
  } else {
    Status = SpiFlashProtocol->Update (SpiFlash,
                                 FlashAddress,
                                 Length,
                                 (UINT8 *)ImageBuffer);
  }
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR,
      "%a: Error while performing flash update\n",
      __FUNCTION__));
    goto FlashProbeError;
  }
 
  DEBUG ((DEBUG_ERROR,
    "%a: Update %d bytes at offset 0x%x succeeded!\n",
    __FUNCTION__,
    Length,
    FlashAddress));
 
  // Release resources
  SpiMasterProtocol->FreeDevice (SpiFlash);
 
  if (!BufferAligned) {
    FreePool (ImageBuffer);
  }
 
  return EFI_SUCCESS;
 
FlashProbeError:
  SpiMasterProtocol->FreeDevice (SpiFlash);
HeaderError:
  if (!BufferAligned) {
    FreePool (ImageBuffer);
  }
 
  return Status;
}
 
/**
  Perform flash write operation.
 
  @param[in] FirmwareType      The type of firmware.
  @param[in] FlashAddress      The address of flash device to be accessed.
  @param[in] FlashAddressType  The type of flash device address.
  @param[in] Buffer            The pointer to the data buffer.
  @param[in] Length            The length of data buffer in bytes.
 
  @retval EFI_SUCCESS           The operation returns successfully.
  @retval EFI_WRITE_PROTECTED   The flash device is read only.
  @retval EFI_UNSUPPORTED       The flash device access is unsupported.
  @retval EFI_INVALID_PARAMETER The input parameter is not valid.
**/
EFI_STATUS
EFIAPI
PerformFlashWrite (
  IN PLATFORM_FIRMWARE_TYPE       FirmwareType,
  IN EFI_PHYSICAL_ADDRESS         FlashAddress,
  IN FLASH_ADDRESS_TYPE           FlashAddressType,
  IN VOID                         *Buffer,
  IN UINTN                        Length
  )
{
  return PerformFlashWriteWithProgress (
           FirmwareType,
           FlashAddress,
           FlashAddressType,
           Buffer,
           Length,
           NULL,
           0,
           0
           );
}