summaryrefslogtreecommitdiff
path: root/EmbeddedPkg/Ebl/HwDebug.c
blob: 1b332228279474e5fe526ec43c9647e1550ad338 (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
/** @file
  Basic command line parser for EBL (Embedded Boot Loader)

  Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
  Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>

  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:  HwDebug.c

  Commands useful for debugging hardware. 

**/

#include "Ebl.h"


/**
  Dump memory

  Argv[0] - "md"[.#] # is optional width 1, 2, 4, or 8. Default 1
  Argv[1] - Hex Address to dump
  Argv[2] - Number of hex bytes to dump (0x20 is default)

  md.4 0x123445678 50 ; Dump 0x50 4 byte quantities starting at 0x123445678
  md   0x123445678 40 ; Dump 0x40 1 byte quantities starting at 0x123445678
  md   0x123445678    ; Dump 0x20 1 byte quantities starting at 0x123445678

  @param  Argc   Number of command arguments in Argv
  @param  Argv   Array of strings that represent the parsed command line. 
                 Argv[0] is the command name

  @return EFI_SUCCESS

**/
EFI_STATUS
EblMdCmd (
  IN UINTN  Argc,
  IN CHAR8  **Argv
  )
{
  STATIC UINT8  *Address = NULL;
  STATIC UINTN  Length   = 0x20;
  STATIC UINTN  Width;

  Width = WidthFromCommandName (Argv[0], 1);

  switch (Argc) {
    case 3:
      Length = AsciiStrHexToUintn(Argv[2]);
    case 2:
      Address = (UINT8 *)AsciiStrHexToUintn (Argv[1]);
    default:
      break;
  }

  OutputData (Address, Length, Width, (UINTN)Address);

  Address += Length;
  
  return EFI_SUCCESS;
}


/**
  Fill Memory with data

  Argv[0] - "mfill"[.#] # is optional width 1, 2, 4, or 8. Default 4
  Argv[1] - Hex Address to fill
  Argv[2] - Data to write (0x00 is default)
  Argv[3] - Number of units to dump.

  mf.1 0x123445678 aa 100 ; Start at 0x123445678 and write aa (1 byte) to the next 100 bytes
  mf.4 0x123445678 aa 100 ; Start at 0x123445678 and write aa (4 byte) to the next 400 bytes
  mf 0x123445678 aa       ; Start at 0x123445678 and write aa (4 byte) to the next 1 byte
  mf 0x123445678          ; Start at 0x123445678 and write 00 (4 byte) to the next 1 byte

  @param  Argc   Number of command arguments in Argv
  @param  Argv   Array of strings that represent the parsed command line. 
                 Argv[0] is the command name

  @return EFI_SUCCESS

**/
EFI_STATUS
EblMfillCmd (
  IN UINTN  Argc,
  IN CHAR8  **Argv
  )
{
  UINTN   Address;
  UINTN   EndAddress;
  UINT32  Data;
  UINTN   Length;
  UINTN   Width;

  if (Argc < 2) {
    return EFI_INVALID_PARAMETER;
  }

  Width = WidthFromCommandName (Argv[0], 4);

  Address = AsciiStrHexToUintn (Argv[1]);
  Data    = (Argc > 2) ? (UINT32)AsciiStrHexToUintn (Argv[2]) : 0;
  Length  = (Argc > 3) ? AsciiStrHexToUintn (Argv[3]) : 1;

  for (EndAddress = Address + (Length * Width); Address < EndAddress; Address += Width) {
    if (Width == 4) {
      MmioWrite32 (Address, Data);
    } else if (Width == 2) {
      MmioWrite16 (Address, (UINT16)Data);
    } else {
      MmioWrite8 (Address, (UINT8)Data);
    }
  }
  
  return EFI_SUCCESS;
}


//
// Strings for PCI Class code [2]
//
CHAR8 *gPciDevClass[] = {
  "Old Device             ",
  "Mass storage           ",
  "Network                ",
  "Display                ",
  "Multimedia             ",
  "Memory controller      ",
  "Bridge device          ",
  "simple communications  ",
  "base system peripherals",
  "Input devices          ",
  "Docking stations       ",
  "Processors             ",
  "serial bus             ",
};


CHAR8 *gPciSerialClassCodes[] = {
  "Mass storage           ",
  "Firewire               ",
  "ACCESS bus             ",
  "SSA                    ",
  "USB                     "
};


/**
  PCI Dump

  Argv[0] - "pci"
  Argv[1] - bus
  Argv[2] - dev
  Argv[3] - func

  @param  Argc   Number of command arguments in Argv
  @param  Argv   Array of strings that represent the parsed command line. 
                 Argv[0] is the command name

  @return EFI_SUCCESS

**/
EFI_STATUS
EblPciCmd (
  IN UINTN  Argc,
  IN CHAR8  **Argv
  )
{
  EFI_STATUS                    Status;
  EFI_PCI_IO_PROTOCOL           *Pci;
  UINTN                         HandleCount;
  EFI_HANDLE                    *HandleBuffer;
  UINTN                         Seg;
  UINTN                         Bus;
  UINTN                         Dev;
  UINTN                         Func;
  UINTN                         BusArg;
  UINTN                         DevArg;
  UINTN                         FuncArg;
  UINTN                         Index;
  UINTN                         Count;
  PCI_TYPE_GENERIC              PciHeader;
  PCI_TYPE_GENERIC              *Header;
  PCI_BRIDGE_CONTROL_REGISTER   *Bridge;
  PCI_DEVICE_HEADER_TYPE_REGION *Device;
  PCI_DEVICE_INDEPENDENT_REGION *Hdr;
  CHAR8                         *Str;
  UINTN                         ThisBus;

  
  BusArg  = (Argc > 1) ? AsciiStrDecimalToUintn (Argv[1]) : 0;
  DevArg  = (Argc > 2) ? AsciiStrDecimalToUintn (Argv[2]) : 0;
  FuncArg = (Argc > 3) ? AsciiStrDecimalToUintn (Argv[3]) : 0;

  Header = &PciHeader;
  
  Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiPciIoProtocolGuid, NULL, &HandleCount, &HandleBuffer);
  if (EFI_ERROR (Status)) {
    AsciiPrint ("No PCI devices found in the system\n");
    return EFI_SUCCESS;
  }

  if (Argc == 1) {
    // Dump all PCI devices
    AsciiPrint ("BusDevFun  VendorId DeviceId  Device Class          Sub-Class\n");
    AsciiPrint ("_____________________________________________________________");
    for (ThisBus = 0; ThisBus <= PCI_MAX_BUS; ThisBus++) {
      for (Index = 0; Index < HandleCount; Index++) {
        Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiPciIoProtocolGuid, (VOID **)&Pci);
        if (!EFI_ERROR (Status)) {
          Pci->GetLocation (Pci, &Seg, &Bus, &Dev, &Func);
          if (ThisBus != Bus) {
            continue;
          }
          AsciiPrint ("\n%03d.%02d.%02d", Bus, Dev, Func);
          Status = Pci->Pci.Read (Pci, EfiPciIoWidthUint32, 0, sizeof (PciHeader)/sizeof (UINT32), &PciHeader);
          if (!EFI_ERROR (Status)) {
            Hdr = &PciHeader.Bridge.Hdr;
            
            if (Hdr->ClassCode[2] < sizeof (gPciDevClass)/sizeof (VOID *)) {
              Str = gPciDevClass[Hdr->ClassCode[2]];
              if (Hdr->ClassCode[2] == PCI_CLASS_SERIAL) {
                if (Hdr->ClassCode[1] < sizeof (gPciSerialClassCodes)/sizeof (VOID *)) {
                  // print out Firewire or USB inplace of Serial Bus controllers
                  Str = gPciSerialClassCodes[Hdr->ClassCode[1]];
                }
              }
            } else {
              Str = "Unknown device         ";
            }
            AsciiPrint ("  0x%04x   0x%04x    %a 0x%02x", Hdr->VendorId, Hdr->DeviceId, Str, Hdr->ClassCode[1]);
          }
          if (Seg != 0) {
            // Only print Segment if it is non zero. If you only have one PCI segment it is 
            // redundent to print it out
            AsciiPrint (" Seg:%d", Seg);
          }
        }
      }
    }
    AsciiPrint ("\n");
  } else {
    // Dump specific PCI device
    for (Index = 0; Index < HandleCount; Index++) {
      Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiPciIoProtocolGuid, (VOID **)&Pci);
      if (!EFI_ERROR (Status)) {
        Pci->GetLocation (Pci, &Seg, &Bus, &Dev, &Func);
        if ((Bus == BusArg) && (Dev == DevArg) && (Func == FuncArg)) {
          // Only print Segment if it is non zero. If you only have one PCI segment it is 
          // redundant to print it out
          if (Seg != 0) {
            AsciiPrint ("Seg:%d ", Seg);
          }
          AsciiPrint ("Bus:%d Dev:%d Func:%d ", Bus, Dev, Func);
          
          Status = Pci->Pci.Read (Pci, EfiPciIoWidthUint32, 0, sizeof (PciHeader)/sizeof (UINT32), Header);
          if (!EFI_ERROR (Status)) {
            Hdr = &PciHeader.Bridge.Hdr;
            if (IS_PCI_BRIDGE (&PciHeader.Bridge)) {
              Bridge = &PciHeader.Bridge.Bridge;
              AsciiPrint (
                "PCI Bridge. Bus Primary %d Secondary %d Subordinate %d\n", 
                Bridge->PrimaryBus, Bridge->SecondaryBus, Bridge->SubordinateBus
                );
              AsciiPrint ("  Bar 0: 0x%08x  Bar 1: 0x%08x\n", Bridge->Bar[0], Bridge->Bar[1]);
            } else {
              Device = &PciHeader.Device.Device;
              AsciiPrint (
                "VendorId: 0x%04x DeviceId: 0x%04x SubSusVendorId: 0x%04x SubSysDeviceId: 0x%04x\n",
                Hdr->VendorId, Hdr->DeviceId, Device->SubsystemVendorID, Device->SubsystemID
                );
              AsciiPrint ("  Class Code: 0x%02x 0x%02x 0x%02x\n", Hdr->ClassCode[2], Hdr->ClassCode[1], Hdr->ClassCode[0]);
              for (Count = 0; Count < 6; Count++) {
                AsciiPrint ("  Bar %d: 0x%08x\n", Count, Device->Bar[Count]);
              }
            }
          }
          
          AsciiPrint ("\n");
          break;
        }
      }
    }
  }
  
  FreePool (HandleBuffer);
  return EFI_SUCCESS;
}


GLOBAL_REMOVE_IF_UNREFERENCED const EBL_COMMAND_TABLE mCmdPciDebugTemplate[] = {
  "pci",
  " [bus] [dev] [func]; Dump PCI",
  NULL,
  EblPciCmd
};


GLOBAL_REMOVE_IF_UNREFERENCED const EBL_COMMAND_TABLE mCmdHwDebugTemplate[] =
{
  {
    "md",
    "[.{1|2|4}] [Addr] [Len] [1|2|4]; Memory Dump from Addr Len bytes",
    NULL,
    EblMdCmd
  },
  {
    "mfill",
    "[.{1|2|4}] Addr Len [data]; Memory Fill Addr Len*(1|2|4) bytes of data(0)",
    NULL,
    EblMfillCmd
  },
};



/**
  Initialize the commands in this in this file
**/
VOID
EblInitializemdHwDebugCmds (
  VOID
  )
{
  if (FeaturePcdGet (PcdEmbeddedHwDebugCmd)) {
    EblAddCommands (mCmdHwDebugTemplate, sizeof (mCmdHwDebugTemplate)/sizeof (EBL_COMMAND_TABLE));
  }
  if (FeaturePcdGet (PcdEmbeddedPciDebugCmd)) {
    EblAddCommands (mCmdPciDebugTemplate, sizeof (mCmdPciDebugTemplate)/sizeof (EBL_COMMAND_TABLE));
  }
}