summaryrefslogtreecommitdiff
path: root/Platform/Marvell/Applications/SpiTool/SpiFlashCmd.c
blob: 184e3d7f54196dac5872b4437efae5c6d83d056d (plain)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*******************************************************************************
Copyright (C) 2016 Marvell International Ltd.

Marvell BSD License Option

If you received this File from Marvell, you may opt to use, redistribute and/or
modify this File under the following licensing terms.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

* Neither the name of Marvell nor the names of its contributors may be
  used to endorse or promote products derived from this software without
  specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/
#include <Uefi.h>
#include <ShellBase.h>

#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/ShellCommandLib.h>
#include <Library/ShellLib.h>
#include <Library/UefiLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/PrintLib.h>
#include <Library/ShellCEntryLib.h>
#include <Library/HiiLib.h>
#include <Library/FileHandleLib.h>

#include <Protocol/Spi.h>
#include <Protocol/SpiFlash.h>

MARVELL_SPI_FLASH_PROTOCOL *SpiFlashProtocol;
MARVELL_SPI_MASTER_PROTOCOL *SpiMasterProtocol;

CONST CHAR16 gShellSpiFlashFileName[] = L"ShellCommand";
EFI_HANDLE gShellSfHiiHandle = NULL;

BOOLEAN InitFlag = 1;

STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
  {L"read", TypeFlag},
  {L"readfile", TypeFlag},
  {L"write", TypeFlag},
  {L"writefile", TypeFlag},
  {L"erase", TypeFlag},
  {L"update", TypeFlag},
  {L"updatefile", TypeFlag},
  {L"probe", TypeFlag},
  {L"help", TypeFlag},
  {NULL , TypeMax}
  };

typedef enum {
  PROBE       = 1,
  READ        = 2,
  READ_FILE   = 4,
  WRITE       = 8,
  WRITE_FILE  = 16,
  ERASE       = 32,
  UPDATE      = 64,
  UPDATE_FILE = 128,
} Flags;

/**
  Return the file name of the help text file if not using HII.

  @return The string pointer to the file name.
**/
CONST CHAR16*
EFIAPI
ShellCommandGetManFileNameSpiFlash (
  VOID
  )
{

  return gShellSpiFlashFileName;
}

VOID
SfUsage (
  VOID
  )
{
  Print (L"\nBasic SPI command\n"
         "sf [probe | read | readfile | write | writefile | erase |"
         "update | updatefile]"
         "[<Address> | <FilePath>] <Offset> <Length>\n\n"
         "Length   - Number of bytes to send\n"
         "Address  - Address in RAM to store/load data\n"
         "FilePath - Path to file to read/write data from/to\n"
         "Offset   - Offset from beggining of SPI flash to store/load data\n"
         "Examples:\n"
         "Check if there is response from SPI flash\n"
         "  sf probe\n"
         "Read 32 bytes from 0xe00000 of SPI flash into RAM at address 0x100000\n"
         "  sf read 0x100000 0xe00000 32\n"
         "Read 0x20 bytes from 0x200000 of SPI flash into RAM at address 0x300000\n"
         "  sf read 0x300000 0x200000 0x20\n"
         "Erase 0x10000 bytes from offset 0x100000 of SPI flash\n"
         "  sf erase 0x100000 0x100000\n"
         "Write 16 bytes from 0x200000 at RAM into SPI flash at address 0x4000000\n"
         "  sf write 0x200000 0x4000000 16\n"
         "Update 100 bytes from 0x100000 at RAM in SPI flash at address 0xe00000\n"
         "  sf update 0x100000 0xe00000 100\n"
         "Read 0x3000 bytes from 0x0 of SPI flash into file fs2:file.bin\n"
         "  sf readfile fs2:file.bin 0x0 0x3000 \n"
         "Update data in SPI flash at 0x3000000 from file Linux.efi\n"
         "  sf updatefile Linux.efi 0x3000000\n"
  );
}

STATIC
EFI_STATUS
OpenAndPrepareFile (
  IN CHAR16 *FilePath,
  SHELL_FILE_HANDLE *FileHandle
  )
{
  EFI_STATUS Status;
  UINT64 OpenMode;

  OpenMode = EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE;

  Status = ShellOpenFileByName (FilePath, FileHandle, OpenMode, 0);
  if (EFI_ERROR (Status)) {
    Print (L"sf: Cannot open file\n");
    return Status;
  }

  Status = FileHandleSetPosition(*FileHandle, 0);

  if (EFI_ERROR(Status)) {
    Print (L"sf: Cannot set file position to first byte\n");
    ShellCloseFile (FileHandle);
    return Status;
  }

  return EFI_SUCCESS;
}

STATIC
EFI_STATUS
FlashProbe (
  IN SPI_DEVICE       *Slave
  )
{
  EFI_STATUS Status;
  UINT8  IdBuffer[4];
  UINT32 Id, RefId;

  Id = PcdGet32 (PcdSpiFlashId);

  IdBuffer[0] = CMD_READ_ID;

  SpiFlashProtocol->ReadId (
    Slave,
    4,
    IdBuffer
    );

  RefId = (IdBuffer[0] << 16) + (IdBuffer[1] << 8) + IdBuffer[2];

  if (RefId == Id) {
    Print (L"sf: Detected supported SPI flash with ID=%3x\n", RefId);
    Status = SpiFlashProtocol->Init (SpiFlashProtocol, Slave);
    if (EFI_ERROR(Status)) {
      Print (L"sf: Cannot initialize flash device\n");
      return SHELL_ABORTED;
    }
    InitFlag = 0;
    return EFI_SUCCESS;
  } else if (RefId != 0) {
    Print (L"sf: Unsupported SPI flash detected with ID=%2x\n", RefId);
    return SHELL_ABORTED;
  }

  Print (L"sf: No SPI flash detected");
  return SHELL_ABORTED;
}

SHELL_STATUS
EFIAPI
ShellCommandRunSpiFlash (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
EFI_STATUS              Status;
  SPI_DEVICE            *Slave;
  LIST_ENTRY            *CheckPackage;
  EFI_PHYSICAL_ADDRESS  Address = 0, Offset = 0;
  SHELL_FILE_HANDLE     FileHandle = NULL;
  UINTN                 ByteCount, FileSize, I;
  UINT8                 *Buffer = NULL, *FileBuffer = NULL;
  CHAR16                *ProblemParam, *FilePath;
  CONST CHAR16          *AddressStr = NULL, *OffsetStr = NULL;
  CONST CHAR16          *LengthStr = NULL, *FileStr = NULL;
  BOOLEAN               AddrFlag = FALSE, LengthFlag = TRUE, FileFlag = FALSE;
  UINT8                 Flag = 0, CheckFlag = 0;

  Status = gBS->LocateProtocol (
    &gMarvellSpiFlashProtocolGuid,
    NULL,
    (VOID **)&SpiFlashProtocol
  );
  if (EFI_ERROR(Status)) {
    Print (L"sf: Cannot locate SpiFlash protocol\n");
    return SHELL_ABORTED;
  }

  Status = gBS->LocateProtocol (
    &gMarvellSpiMasterProtocolGuid,
    NULL,
    (VOID **)&SpiMasterProtocol
  );
  if (EFI_ERROR(Status)) {
    Print (L"sf: Cannot locate SpiMaster protocol\n");
    return SHELL_ABORTED;
  }

  // Parse Shell command line
  Status = ShellInitialize ();
  if (EFI_ERROR (Status)) {
    Print (L"sf: Cannot initialize Shell\n");
    ASSERT_EFI_ERROR (Status);
    return SHELL_ABORTED;
  }

  Status = ShellCommandLineParse (ParamList, &CheckPackage, &ProblemParam, TRUE);
  if (EFI_ERROR (Status)) {
    Print (L"sf: Error while parsing command line\n");
    return SHELL_ABORTED;
  }

  if (ShellCommandLineGetFlag (CheckPackage, L"help")) {
    SfUsage();
    return EFI_SUCCESS;
  }

  // Check flags provided by user
  Flag |= (ShellCommandLineGetFlag (CheckPackage, L"probe") << 0);
  Flag |= (ShellCommandLineGetFlag (CheckPackage, L"read") << 1);
  Flag |= (ShellCommandLineGetFlag (CheckPackage, L"readfile") << 2);
  Flag |= (ShellCommandLineGetFlag (CheckPackage, L"write") << 3);
  Flag |= (ShellCommandLineGetFlag (CheckPackage, L"writefile") << 4);
  Flag |= (ShellCommandLineGetFlag (CheckPackage, L"erase") << 5);
  Flag |= (ShellCommandLineGetFlag (CheckPackage, L"update") << 6);
  Flag |= (ShellCommandLineGetFlag (CheckPackage, L"updatefile") << 7);

  if (InitFlag && !(Flag & PROBE)) {
    Print (L"Please run sf probe\n");
    return EFI_SUCCESS;
  }

  CheckFlag = Flag;
  for (I = 0; CheckFlag; CheckFlag >>= 1) {
    I += CheckFlag & 1;
    if (I > 1) {
      Print (L"sf: Too many flags\n");
      SfUsage();
      return SHELL_ABORTED;
    }
  }

  // Setup new spi device
  Slave = SpiMasterProtocol->SetupDevice (SpiMasterProtocol, 0, 0);
    if (Slave == NULL) {
      Print(L"sf: Cannot allocate SPI device!\n");
      return SHELL_ABORTED;
    }

  switch (Flag) {
  case PROBE:
    // Probe spi bus
    Status = FlashProbe (Slave);
    if (EFI_ERROR(Status)) {
      // No supported spi flash detected
      return SHELL_ABORTED;
    } else {
      return Status;
    }
    break;
  // Fall through
  case READ:
  case WRITE:
  case UPDATE:
    AddressStr = ShellCommandLineGetRawValue (CheckPackage, 1);
    OffsetStr = ShellCommandLineGetRawValue (CheckPackage, 2);
    LengthStr = ShellCommandLineGetRawValue (CheckPackage, 3);
    AddrFlag = TRUE;
    break;
  case ERASE:
    OffsetStr = ShellCommandLineGetRawValue (CheckPackage, 1);
    LengthStr = ShellCommandLineGetRawValue (CheckPackage, 2);
    break;
  case READ_FILE:
    FileStr = ShellCommandLineGetRawValue (CheckPackage, 1);
    OffsetStr = ShellCommandLineGetRawValue (CheckPackage, 2);
    LengthStr = ShellCommandLineGetRawValue (CheckPackage, 3);
    FileFlag = TRUE;
    break;
  case WRITE_FILE:
  case UPDATE_FILE:
    FileStr = ShellCommandLineGetRawValue (CheckPackage, 1);
    OffsetStr = ShellCommandLineGetRawValue (CheckPackage, 2);
    LengthFlag = FALSE;
    FileFlag = TRUE;
    break;
  }

  // Read address parameter
  if ((AddressStr == NULL) & AddrFlag) {
    Print (L"sf: No address parameter!\n");
    return SHELL_ABORTED;
  } else if (AddrFlag) {
    Address = ShellHexStrToUintn (AddressStr);
    if (Address == (UINTN)(-1)) {
      Print (L"sf: Wrong address parameter\n");
      return SHELL_ABORTED;
    }
  }

  // Read offset parameter
  if (OffsetStr == NULL) {
    Print (L"sf: No offset Parameter!\n");
    return SHELL_ABORTED;
  } else {
    Offset = ShellHexStrToUintn (OffsetStr);
    if (Offset < 0) {
      Print (L"sf: Wrong offset parameter: %s", OffsetStr);
      return SHELL_ABORTED;
    }
  }

  // Read length parameter
  if ((LengthStr == NULL) & LengthFlag) {
    Print (L"sf: No lenght parameter!\n");
    return SHELL_ABORTED;
  } else if (LengthFlag) {
    ByteCount = ShellStrToUintn (LengthStr);
    if (ByteCount < 0) {
      Print (L"sf: Wrong length parameter %s!\n", LengthStr);
      return SHELL_ABORTED;
    }
  }

  if (FileFlag) {
    // Read FilePath parameter
    if (FileStr == NULL) {
      Print (L"sf: No FilePath parameter!\n");
      return SHELL_ABORTED;
    } else {
      FilePath = (CHAR16 *) FileStr;
      Status = ShellIsFile (FilePath);
      // When read file into flash, file doesn't have to exist
      if (EFI_ERROR(Status && !(Flag & READ_FILE))) {
        Print (L"sf: Wrong FilePath parameter!\n");
        return SHELL_ABORTED;
      }
    }

    Status = OpenAndPrepareFile (FilePath, &FileHandle);
    if (EFI_ERROR(Status)) {
      Print (L"sf: Error while preparing file\n");
      return SHELL_ABORTED;
    }

    // Get file size in order to check correctness at the end of transfer
    if (Flag & (WRITE_FILE | UPDATE_FILE)) {
      Status = FileHandleGetSize (FileHandle, &FileSize);
      if (EFI_ERROR (Status)) {
        Print (L"sf: Cannot get file size\n");
      }
      ByteCount = (UINTN) FileSize;
    }

    FileBuffer = AllocateZeroPool ((UINTN) ByteCount);
    if (FileBuffer == NULL) {
      Print (L"sf: Cannot allocate memory\n");
      goto Error_Close_File;
    }

    // Read file content and store it in FileBuffer
    if (Flag & (WRITE_FILE | UPDATE_FILE)) {
      Status = FileHandleRead (FileHandle, &ByteCount, FileBuffer);
      if (EFI_ERROR (Status)) {
        Print (L"sf: Read from file error\n");
        goto Error_Free_Buffer;
      } else if (ByteCount != (UINTN) FileSize) {
        Print (L"sf: Not whole file read. Abort\n");
        goto Error_Free_Buffer;
      }
    }
  }

  Buffer = (UINT8 *) Address;
  if (FileFlag) {
    Buffer = FileBuffer;
  }

  switch (Flag) {
  case READ:
  case READ_FILE:
    Status = SpiFlashProtocol->Read (Slave, Offset, ByteCount, Buffer);
    break;
  case ERASE:
    Status = SpiFlashProtocol->Erase (Slave, Offset, ByteCount);
    break;
  case WRITE:
  case WRITE_FILE:
    Status = SpiFlashProtocol->Write (Slave, Offset, ByteCount, Buffer);
    break;
  case UPDATE:
  case UPDATE_FILE:
    Status = SpiFlashProtocol->Update (Slave, Offset, ByteCount, Buffer);
    break;
  }

  SpiMasterProtocol->FreeDevice(Slave);

  if (EFI_ERROR (Status)) {
    Print (L"sf: Error while performing spi transfer\n");
    return SHELL_ABORTED;
  }

  switch (Flag) {
  case ERASE:
    Print (L"sf: %d bytes succesfully erased at offset 0x%x\n", ByteCount,
      Offset);
    break;
  case WRITE:
  case WRITE_FILE:
    Print (L"sf: Write %d bytes at offset 0x%x\n", ByteCount, Offset);
    break;
  case UPDATE:
  case UPDATE_FILE:
    Print (L"sf: Update %d bytes at offset 0x%x\n", ByteCount, Offset);
    break;
  case READ:
    Print (L"sf: Read %d bytes from offset 0x%x\n", ByteCount, Offset);
    break;
  case READ_FILE:
    Status = FileHandleWrite (FileHandle, &ByteCount, FileBuffer);
    if (EFI_ERROR(Status)) {
      Print (L"sf: Error while writing into file\n");
      goto Error_Free_Buffer;
    }
    break;
  }

  if (FileFlag) {
    FreePool (FileBuffer);

    if (FileHandle != NULL) {
      ShellCloseFile (&FileHandle);
    }
  }

  return EFI_SUCCESS;

Error_Free_Buffer:
  FreePool (FileBuffer);
Error_Close_File:
  ShellCloseFile (&FileHandle);
  return SHELL_ABORTED;
}

EFI_STATUS
EFIAPI
ShellSpiFlashLibConstructor (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  gShellSfHiiHandle = NULL;

  gShellSfHiiHandle = HiiAddPackages (
                        &gShellSfHiiGuid, gImageHandle,
                        UefiShellSpiFlashLibStrings, NULL
                        );
  if (gShellSfHiiHandle == NULL) {
    return EFI_DEVICE_ERROR;
  }

  ShellCommandRegisterCommandName (
     L"sf", ShellCommandRunSpiFlash, ShellCommandGetManFileNameSpiFlash, 0,
     L"sf", TRUE , gShellSfHiiHandle, STRING_TOKEN (STR_GET_HELP_SF)
     );

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
ShellSpiFlashLibDestructor (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{

  if (gShellSfHiiHandle != NULL) {
    HiiRemovePackages (gShellSfHiiHandle);
  }
  return EFI_SUCCESS;
}