File: MemCommonPart.c

package info (click to toggle)
edk2 0~20131112.2590861a-3
  • links: PTS, VCS
  • area: non-free
  • in suites: jessie, jessie-kfreebsd
  • size: 125,836 kB
  • ctags: 175,818
  • sloc: ansic: 1,274,042; python: 75,968; asm: 68,082; perl: 22,386; cpp: 22,278; makefile: 14,914; sh: 4,113; pascal: 1,126; xml: 318; lisp: 24
file content (348 lines) | stat: -rw-r--r-- 9,092 bytes parent folder | download | duplicates (3)
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
/*++
 
Copyright (c) 2005, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution. The full text of the license may be found at         
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:

  MemCommonPart.c
  
Abstract: 

  shared code for shell command "dmem/mem".

Revision History

--*/

#include "MemCommonPart.h"

extern UINT8  STRING_ARRAY_NAME[];

//
// This is the generated header file which includes whatever needs to be exported (strings + IFR)
//
#include STRING_DEFINES_FILE

//
// Global Variables
//
EFI_HII_HANDLE  HiiMemHandle;
EFI_GUID        EfiMemGuid = EFI_MEM_GUID;
SHELL_VAR_CHECK_ITEM    MemCheckList[] = {
  {
    L"-MMIO",
    0x01,
    0x04,
    FlagTypeSingle
  },
  {
    L"-B",
    0x02,
    0,
    FlagTypeSingle
  },
  {
    L"-?",
    0x04,
    0x01,
    FlagTypeSingle
  },
  {
    NULL,
    0,
    0,
    (SHELL_VAR_CHECK_FLAG_TYPE) 0
  }
};

EFI_STATUS
MemMainProcOld (
  IN EFI_HANDLE           ImageHandle,
  IN EFI_SYSTEM_TABLE     *SystemTable
  )
/*++
Routine Description:

  mem [Address] [Size] ;MMIO
    if no Address default address is EFI System Table
    if no size default size is 512;
    if ;MMIO then use memory mapped IO and not system memory
    
Arguments:

  ImageHandle - The image handle
  SystemTable - The system table

Returns:

  EFI_SUCCESS - Success

--*/
{
  EFI_STATUS                      Status;
  EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;
  UINT64                          Address;
  UINTN                           Size;
  UINT8                           *Buffer;
  BOOLEAN                         MMIo;
  UINTN                           Index;
  CHAR16                          *AddressStr;
  CHAR16                          *SizeStr;
  CHAR16                          *Ptr;
  BOOLEAN                         PrtHelp;

  Status  = EFI_SUCCESS;
  PrtHelp = FALSE;
  //
  // Get command arguments
  //
  MMIo                  = FALSE;
  AddressStr            = NULL;
  SizeStr               = NULL;
  for (Index = 1; Index < SI->Argc; Index += 1) {
    Ptr = SI->Argv[Index];
    if (*Ptr == ';') {
      //
      // Shortcut! assume MMIo if ; exists
      //
      if (StriCmp (Ptr, L";MMIO") == 0) {
        MMIo = TRUE;
        continue;
      } else {
        PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_UNKNOWN_FLAG), HiiMemHandle, L"mem", Ptr);
        Status = EFI_INVALID_PARAMETER;
        return Status;
      }
    } else if (*Ptr == '-') {
      if (0 == StriCmp (Ptr, L"-b")) {
        EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
        continue;
      } else if (0 == StriCmp (Ptr, L"-?")) {
        PrtHelp = TRUE;
      } else {
        PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_UNKNOWN_FLAG), HiiMemHandle, L"mem", Ptr);
        Status = EFI_INVALID_PARAMETER;
        return Status;
      }
    }

    if (!AddressStr) {
      AddressStr = Ptr;
      continue;
    }

    if (!SizeStr) {
      SizeStr = Ptr;
      continue;
    }
  }

  if (PrtHelp) {
    PrintToken (STRING_TOKEN (STR_MEM_VERBOSEHELP), HiiMemHandle);
    return EFI_SUCCESS;
  }

  Address = (AddressStr) ? Xtoi (AddressStr) : (UINT64)(UINTN) SystemTable;
  Size    = (SizeStr) ? Xtoi (SizeStr) : 512;

  //
  // Get memory data
  //
  PrintToken (STRING_TOKEN (STR_MEM_MEMORY_ADDR), HiiMemHandle, 2 * sizeof (UINTN), Address, Size);
  if (MMIo) {
    Status = BS->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid, NULL, (VOID**)&PciRootBridgeIo);
    if (EFI_ERROR (Status)) {
      PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_LOC_PROT_ERR_EX), HiiMemHandle, L"mem", L"PciRootBridgeIo");
      return Status;
    }
    //
    // Allocate buffer for memory io read
    //
    Buffer = AllocatePool (Size);
    if (Buffer == NULL) {
      PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_OUT_RESOURCE), HiiMemHandle, L"mem");
      Status = EFI_OUT_OF_RESOURCES;
      return Status;
    }

    PciRootBridgeIo->Mem.Read (PciRootBridgeIo, EfiPciWidthUint8, Address, Size, Buffer);
  } else {
    Buffer = (UINT8 *) (UINTN) Address;
  }
  //
  // Dump data
  //
  DumpHex (2, (UINTN) Address, Size, Buffer);
  EFIMemStructsPrint (Buffer, Size, Address, NULL);

  if (MMIo) {
    FreePool (Buffer);
  }

  return Status;
}

EFI_STATUS
MemMainProc (
  IN EFI_HANDLE           ImageHandle,
  IN EFI_SYSTEM_TABLE     *SystemTable
  )
/*+++
Routine Description:

  mem [Address] [Size] -MMIO
    if no Address default address is EFI System Table
    if no size default size is 512;
    if -MMIO then use memory mapped IO and not system memory
    
Arguments:

  ImageHandle - The image handle
  SystemTable - The system table
  
Returns:

--*/
{
  EFI_STATUS                      Status;
  EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;
  UINT64                          Address;
  UINTN                           Size;
  UINT8                           *Buffer;
  BOOLEAN                         MMIo;
  CHAR16                          *AddressStr;
  CHAR16                          *SizeStr;
  SHELL_VAR_CHECK_CODE            RetCode;
  CHAR16                          *Useful;
  SHELL_ARG_LIST                  *Item;
  SHELL_VAR_CHECK_PACKAGE         ChkPck;

  Status  = EFI_SUCCESS;
  ZeroMem (&ChkPck, sizeof (SHELL_VAR_CHECK_PACKAGE));

  //
  // Get command arguments
  //
  MMIo                  = FALSE;
  AddressStr            = NULL;
  SizeStr               = NULL;
  Address               = (UINT64)(UINTN) SystemTable;
  Size                  = 512;
  RetCode               = LibCheckVariables (SI, MemCheckList, &ChkPck, &Useful);
  if (VarCheckOk != RetCode) {
    switch (RetCode) {
    case VarCheckUnknown:
      PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_UNKNOWN_FLAG), HiiMemHandle, L"mem", Useful);
      break;

    case VarCheckConflict:
      PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_FLAG_CONFLICT), HiiMemHandle, L"mem", Useful);
      break;

    case VarCheckDuplicate:
      PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_DUP_FLAG), HiiMemHandle, L"mem", Useful);
      break;

    default:
      break;
    }

    Status = EFI_INVALID_PARAMETER;
    goto Done;
  }

  if (LibCheckVarGetFlag (&ChkPck, L"-b")) {
    EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
  }

  if (LibCheckVarGetFlag (&ChkPck, L"-?")) {
    if (ChkPck.ValueCount > 0 ||
        ChkPck.FlagCount > 2 ||
        (2 == ChkPck.FlagCount && !LibCheckVarGetFlag (&ChkPck, L"-b"))
        ) {
      PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_TOO_MANY), HiiMemHandle, L"mem");
      Status = EFI_INVALID_PARAMETER;
    } else {
      PrintToken (STRING_TOKEN (STR_MEM_VERBOSEHELP), HiiMemHandle);
      Status = EFI_SUCCESS;
    }

    goto Done;
  }

  if (LibCheckVarGetFlag (&ChkPck, L"-MMIO")) {
    MMIo = TRUE;
  }

  Item = ChkPck.VarList;
  while (NULL != Item) {
    if (!AddressStr) {
      AddressStr  = Item->VarStr;
      Address     = StrToUIntegerBase (AddressStr, 16, &Status);
      if (EFI_ERROR (Status) || Address == 0) {
        PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_INVALID_ARG), HiiMemHandle, L"mem", AddressStr);
        Status = EFI_INVALID_PARAMETER;
        goto Done;
      }

      Item = Item->Next;
      continue;
    }

    if (!SizeStr) {
      SizeStr = Item->VarStr;
      Size    = (UINTN) StrToUIntegerBase (SizeStr, 10, &Status);
      if (EFI_ERROR (Status) || Size == 0) {
        PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_INVALID_ARG), HiiMemHandle, L"mem", SizeStr);
        Status = EFI_INVALID_PARAMETER;
        goto Done;
      }
      break;
    }
  }
  //
  // Get memory data
  //
  PrintToken (STRING_TOKEN (STR_MEM_NEW_MEMORY_ADDR), HiiMemHandle, 2 * sizeof (UINTN), Address, Size);
  if (MMIo) {
    Status = BS->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid, NULL, (VOID**)&PciRootBridgeIo);
    if (EFI_ERROR (Status)) {
      PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_LOC_PROT_ERR_EX), HiiMemHandle, L"mem", L"PciRootBridgeIo");
      return Status;
    }
    //
    // Allocate buffer for memory io read
    //
    Buffer = AllocatePool (Size);
    if (Buffer == NULL) {
      PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_OUT_RESOURCE), HiiMemHandle, L"mem");
      Status = EFI_OUT_OF_RESOURCES;
      return Status;
    }

    PciRootBridgeIo->Mem.Read (PciRootBridgeIo, EfiPciWidthUint8, Address, Size, Buffer);
  } else {
    Buffer = (UINT8 *) (UINTN) Address;
  }
  //
  // Dump data
  //
  DumpHex (2, (UINTN) Address, Size, Buffer);
  EFIMemStructsPrint (Buffer, Size, Address, NULL);

  if (MMIo) {
    FreePool (Buffer);
  }

Done:

  LibCheckVarFreeVarList (&ChkPck);
  return Status;
}