• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /********************************************************************************
2 Copyright (C) 2016 Marvell International Ltd.
3 
4 Marvell BSD License Option
5 
6 If you received this File from Marvell, you may opt to use, redistribute and/or
7 modify this File under the following licensing terms.
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
10 
11 * Redistributions of source code must retain the above copyright notice,
12   this list of conditions and the following disclaimer.
13 
14 * Redistributions in binary form must reproduce the above copyright
15   notice, this list of conditions and the following disclaimer in the
16   documentation and/or other materials provided with the distribution.
17 
18 * Neither the name of Marvell nor the names of its contributors may be
19   used to endorse or promote products derived from this software without
20   specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 *******************************************************************************/
34 
35 #include <Uefi.h>
36 
37 #include <ShellBase.h>
38 #include <Library/BaseLib.h>
39 #include <Library/BaseMemoryLib.h>
40 #include <Library/DebugLib.h>
41 #include <Library/MemoryAllocationLib.h>
42 #include <Library/ShellCommandLib.h>
43 #include <Library/ShellLib.h>
44 #include <Library/UefiLib.h>
45 #include <Library/UefiRuntimeServicesTableLib.h>
46 #include <Library/UefiBootServicesTableLib.h>
47 #include <Library/PrintLib.h>
48 #include <Library/HiiLib.h>
49 
50 #include <Library/UefiLib.h>
51 #include <Library/ShellCEntryLib.h>
52 #include <Library/UefiBootServicesTableLib.h>
53 
54 #include <Protocol/Eeprom.h>
55 
56 #define I2C_DEVICE_INDEX(bus, address) (((address) & 0xffff) | (bus) << 16)
57 #define I2C_DEVICE_ADDRESS(index) ((index) & 0xffff)
58 #define I2C_DEVICE_BUS(index) ((index) >> 16)
59 
60 CONST CHAR16 ShellEepromFileName[] = L"ShellCommand";
61 EFI_HANDLE ShellEepromHiiHandle = NULL;
62 
63 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
64   {L"-d", TypeValue},
65   {L"-m", TypeValue},
66   {L"read", TypeFlag},
67   {L"write", TypeFlag},
68   {L"list", TypeFlag},
69   {L"help", TypeFlag},
70   {NULL , TypeMax}
71   };
72 
73 /**
74   Return the file name of the help text file if not using HII.
75 
76   @return The string pointer to the file name.
77 **/
78 STATIC
79 CONST CHAR16*
80 EFIAPI
ShellCommandGetManFileNameEeprom(VOID)81 ShellCommandGetManFileNameEeprom (
82   VOID
83   )
84 {
85   return ShellEepromFileName;
86 }
87 
88 STATIC
89 VOID
Usage(VOID)90 Usage (
91   VOID
92   )
93 {
94   Print (L"Basic EEPROM commands:\n"
95          "eeprom [read] [write] [list] [<Chip>] [<Bus>][<Address>] [<Length>] [-d <Data>] [-m <Source>]\n"
96          "All modes except 'list' require Address, Length and Chip set.\n\n"
97          "read    - read from EEPROM\n"
98          "write   - write Data to EEPROM, requires -d\n"
99          "list    - list available EEPROM devices\n\n"
100          "-m      - transfer from/to RAM memory\n\n"
101          "Chip    - EEPROM bus address\n"
102          "Bus     - I2C bus address\n"
103          "Address - address in EEPROM to read/write\n"
104          "Data    - data byte to be written\n"
105          "Length  - length of data to read/write/copy\n"
106          "Source  - address of data in RAM to be copied\n"
107          "Examples:\n"
108          "List devices:\n"
109          " eeprom list\n"
110          "Read 16 bytes from address 0x0 in chip 0x57:\n"
111          " eeprom read 0x57 0x0 0x0 0x10\n"
112          "Fill 16 bytes with 0xab at address 0x0 in chip 0x57:\n"
113          " eeprom write 0x57 0x0 0x0 0x10 -d 0xab\n"
114          "Copy 32 bytes from 0x2000000 in RAM to EEPROM:\n"
115          " eeprom write 0x57 0x0 0x0 0x20 -m 0x2000000\n"
116          "Copy 32 bytes from EEPROM to RAM:\n"
117          " eeprom read 0x57 0x0 0x0 0x20 -m 0x2000000\n"
118   );
119 }
120 
121 STATIC
122 EFI_STATUS
EepromList()123 EepromList (
124     )
125 {
126   EFI_HANDLE *HandleBuffer;
127   EFI_STATUS Status;
128   UINTN ProtocolCount;
129   MARVELL_EEPROM_PROTOCOL *EepromProtocol;
130   UINTN i;
131   Status = gBS->LocateHandleBuffer ( ByProtocol,
132                                      &gMarvellEepromProtocolGuid,
133                                      NULL,
134                                      &ProtocolCount,
135                                      &HandleBuffer
136                                    );
137   if (ProtocolCount == 0) {
138     Print (L"0 devices found.\n");
139   } else {
140     Print (L"%u devices found: ", ProtocolCount);
141   }
142 
143   for (i = 0; i < ProtocolCount; i++) {
144     Status = gBS->OpenProtocol (
145                     HandleBuffer[i],
146                     &gMarvellEepromProtocolGuid,
147                     (VOID **) &EepromProtocol,
148                     gImageHandle,
149                     NULL,
150                     EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL );
151     Print (L"0x%x at bus %d\n", I2C_DEVICE_ADDRESS(EepromProtocol->Identifier),
152         I2C_DEVICE_BUS(EepromProtocol->Identifier));
153     Status = gBS->CloseProtocol ( HandleBuffer[i],
154                                   &gMarvellEepromProtocolGuid,
155                                   gImageHandle,
156                                   NULL );
157   }
158   Print (L"\n");
159   return Status;
160 }
161 
162 STATIC
163 EFI_STATUS
EepromLocateProtocol(IN UINT32 Identifier,OUT EFI_HANDLE * FoundHandle,OUT MARVELL_EEPROM_PROTOCOL ** FoundEepromProtocol)164 EepromLocateProtocol (
165  IN  UINT32 Identifier,
166  OUT EFI_HANDLE *FoundHandle,
167  OUT MARVELL_EEPROM_PROTOCOL **FoundEepromProtocol
168  )
169 {
170   EFI_HANDLE *HandleBuffer;
171   EFI_STATUS Status;
172   UINTN ProtocolCount;
173   MARVELL_EEPROM_PROTOCOL *EepromProtocol;
174   UINTN i;
175   Status = gBS->LocateHandleBuffer ( ByProtocol,
176                                      &gMarvellEepromProtocolGuid,
177                                      NULL,
178                                      &ProtocolCount,
179                                      &HandleBuffer
180                                    );
181   if (EFI_ERROR(Status))
182     return Status;
183 
184   for (i = 0; i < ProtocolCount; i++) {
185     Status = gBS->OpenProtocol (
186                     HandleBuffer[i],
187                     &gMarvellEepromProtocolGuid,
188                     (VOID **) &EepromProtocol,
189                     gImageHandle,
190                     NULL,
191                     EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL );
192     if (EepromProtocol->Identifier == Identifier) {
193       *FoundEepromProtocol = EepromProtocol;
194       *FoundHandle = HandleBuffer[i];
195       return EFI_SUCCESS;
196     }
197     Status = gBS->CloseProtocol ( HandleBuffer[i],
198                                   &gMarvellEepromProtocolGuid,
199                                   gImageHandle,
200                                   NULL );
201   }
202   *FoundEepromProtocol = NULL;
203   return EFI_UNSUPPORTED;
204 }
205 
206 SHELL_STATUS
207 EFIAPI
ShellCommandRunEeprom(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)208 ShellCommandRunEeprom (
209   IN EFI_HANDLE        ImageHandle,
210   IN EFI_SYSTEM_TABLE  *SystemTable
211   )
212 {
213   EFI_STATUS              Status;
214   LIST_ENTRY              *CheckPackage;
215   CHAR16                  *ProblemParam;
216   CONST CHAR16            *ValueStr;
217   UINTN                   Address, Length, Chip, Source, Bus;
218   UINT8                   Data;
219   UINT8                   *Buffer;
220   BOOLEAN                 ReadMode, MemMode;
221   EFI_HANDLE              Handle, ProtHandle;
222   MARVELL_EEPROM_PROTOCOL *EepromProtocol = NULL;
223   UINTN                   Count, HandleSize;
224 
225   Handle = NULL;
226   Source = 0;
227   HandleSize = 2 * sizeof (EFI_HANDLE);
228 
229   Status = gBS->LocateHandle (ByProtocol, &gMarvellEepromProtocolGuid, NULL,
230     &HandleSize, &ProtHandle);
231   if (EFI_ERROR(Status)) {
232     DEBUG((DEBUG_INFO, "No Eeprom protocol, connect I2C stack\n"));
233     Status = gBS->LocateHandle (ByProtocol, &gEfiI2cMasterProtocolGuid, NULL,
234       &HandleSize, &ProtHandle);
235     if (EFI_ERROR(Status)) {
236       Print (L"Failed to locate I2cMaster protocol, abort!\n");
237       return SHELL_ABORTED;
238     }
239     Status = gBS->ConnectController (ProtHandle, NULL, NULL, TRUE);
240     if (EFI_ERROR(Status)) {
241       Print (L"Cannot connect I2C stack, abort!\n");
242       return SHELL_ABORTED;
243     }
244   }
245 
246   Status = ShellInitialize ();
247   if (EFI_ERROR (Status)) {
248     Print (L"Error - failed to initialize shell\n");
249     return SHELL_ABORTED;
250   }
251 
252   Status = ShellCommandLineParse (ParamList, &CheckPackage, &ProblemParam, TRUE);
253   if (EFI_ERROR (Status)) {
254     Print (L"Error - failed to parse command line\n");
255     return SHELL_ABORTED;
256   }
257 
258   if (ShellCommandLineGetFlag (CheckPackage, L"list")) {
259     EepromList();
260     return SHELL_SUCCESS;
261   }
262 
263   if (ShellCommandLineGetFlag (CheckPackage, L"help")) {
264     Usage();
265     return SHELL_SUCCESS;
266   }
267 
268   if (ShellCommandLineGetCount(CheckPackage) < 4) {
269     Print (L"Not enough arguments given.\n");
270     Usage();
271   }
272 
273   ReadMode = ShellCommandLineGetFlag (CheckPackage, L"read");
274   if (ReadMode == ShellCommandLineGetFlag (CheckPackage, L"write")) {
275     Print (L"Error - type read, write, list or help.\n");
276     return SHELL_INVALID_PARAMETER;
277   }
278 
279   MemMode = ShellCommandLineGetFlag (CheckPackage, L"-m");
280   Data = 0;
281   if (!ReadMode && !MemMode) {
282     ValueStr = ShellCommandLineGetValue (CheckPackage, L"-d");
283     if (ValueStr == NULL) {
284       Print (L"Error - no data to write.\n");
285       return SHELL_INVALID_PARAMETER;
286     }
287     Data = ShellHexStrToUintn (ValueStr);
288   }
289 
290   ValueStr = ShellCommandLineGetRawValue(CheckPackage, 1);
291   Chip = ShellHexStrToUintn (ValueStr);
292 
293   ValueStr = ShellCommandLineGetRawValue(CheckPackage, 2);
294   Bus = ShellHexStrToUintn (ValueStr);
295 
296   ValueStr = ShellCommandLineGetRawValue(CheckPackage, 3);
297   Address = ShellHexStrToUintn (ValueStr);
298 
299   ValueStr = ShellCommandLineGetRawValue(CheckPackage, 4);
300   Length = ShellHexStrToUintn (ValueStr);
301 
302   if (MemMode) {
303     ValueStr = ShellCommandLineGetValue (CheckPackage, L"-m");
304     if (ValueStr == NULL) {
305       Print (L"Error - no memory address given.\n");
306       return SHELL_INVALID_PARAMETER;
307     }
308     Source = ShellHexStrToUintn (ValueStr);
309   }
310 
311   EepromLocateProtocol (I2C_DEVICE_INDEX(Bus, Chip), &Handle, &EepromProtocol);
312   if (EepromProtocol == NULL) {
313     Print (L"Failed to locate EEPROM protocol.\n");
314     return SHELL_INVALID_PARAMETER;
315   }
316 
317   if (MemMode) {
318     Buffer = (VOID *) Source;
319   } else {
320     Buffer = AllocateZeroPool (Length);
321     if (Buffer == NULL) {
322       Status = SHELL_OUT_OF_RESOURCES;
323       Print (L"Error - out of resources.\n");
324       goto out_close;
325     }
326     if (!ReadMode) {
327       for (Count = 0; Count < Length; Count++)
328         Buffer[Count] = Data;
329     }
330   }
331   EepromProtocol->Transfer(EepromProtocol, Address, Length, Buffer,
332       ReadMode ? EEPROM_READ : EEPROM_WRITE);
333 
334   if (MemMode) {
335   Print (L"Transfered succesfully.\n");
336   } else {
337     Print (L"Transfered:\n");
338     for (Count = 0; Count < Length; Count++) {
339       Print(L"0x%x ", Buffer[Count]);
340       if (Count % 8 == 7)
341         Print(L"\n");
342     }
343     Print (L"\n");
344   }
345 
346   Status = SHELL_SUCCESS;
347 
348   if (!MemMode)
349     FreePool(Buffer);
350 out_close:
351   gBS->CloseProtocol ( Handle,
352                        &gMarvellEepromProtocolGuid,
353                        gImageHandle,
354                        NULL );
355 
356   return Status;
357 }
358 
359 EFI_STATUS
360 EFIAPI
ShellEepromCmdLibConstructor(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)361 ShellEepromCmdLibConstructor (
362   IN EFI_HANDLE        ImageHandle,
363   IN EFI_SYSTEM_TABLE  *SystemTable
364   )
365 {
366 
367   ShellEepromHiiHandle = NULL;
368 
369   ShellEepromHiiHandle = HiiAddPackages (
370                           &gShellEepromHiiGuid, gImageHandle,
371                           UefiShellEepromLibStrings, NULL
372                           );
373   if (ShellEepromHiiHandle == NULL) {
374     Print (L"Filed to add Hii package\n");
375     return EFI_DEVICE_ERROR;
376   }
377   ShellCommandRegisterCommandName (
378      L"eeprom", ShellCommandRunEeprom, ShellCommandGetManFileNameEeprom, 0,
379      L"eeprom", TRUE , ShellEepromHiiHandle, STRING_TOKEN (STR_GET_HELP_EEPROM)
380      );
381 
382   return EFI_SUCCESS;
383 }
384 
385 EFI_STATUS
386 EFIAPI
ShellEepromCmdLibDestructor(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)387 ShellEepromCmdLibDestructor (
388   IN EFI_HANDLE        ImageHandle,
389   IN EFI_SYSTEM_TABLE  *SystemTable
390   )
391 {
392 
393   if (ShellEepromHiiHandle != NULL) {
394     HiiRemovePackages (ShellEepromHiiHandle);
395   }
396   return EFI_SUCCESS;
397 }
398