1 /** @file
2 
3 Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution.  The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8 
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 
12 
13 **/
14 
15 #include "Edb.h"
16 
17 
18 /**
19 
20   Display memory unit.
21 
22   @param  Address         - Memory Address
23   @param  Width           - Memory Width
24 
25   @return Length of the memory unit
26 
27 **/
28 UINTN
EdbDisplayMemoryUnit(IN UINTN Address,IN EDB_DATA_WIDTH Width)29 EdbDisplayMemoryUnit (
30   IN UINTN           Address,
31   IN EDB_DATA_WIDTH  Width
32   )
33 {
34   UINT8  Data8;
35   UINT16 Data16;
36   UINT32 Data32;
37   UINT64 Data64;
38 
39   //
40   // Print accroding to width
41   //
42   switch (Width) {
43   case EdbWidthUint8:
44     CopyMem (&Data8, (VOID *)Address, sizeof(UINT8));
45     EDBPrint (L"%02x ", Data8);
46     return sizeof(UINT8);
47   case EdbWidthUint16:
48     CopyMem (&Data16, (VOID *)Address, sizeof(UINT16));
49     EDBPrint (L"%04x ", Data16);
50     return sizeof(UINT16);
51   case EdbWidthUint32:
52     CopyMem (&Data32, (VOID *)Address, sizeof(UINT32));
53     EDBPrint (L"%08x ", Data32);
54     return sizeof(UINT32);
55   case EdbWidthUint64:
56     CopyMem (&Data64, (VOID *)Address, sizeof(UINT64));
57     EDBPrint (L"%016lx ", Data64);
58     return sizeof(UINT64);
59   default:
60     ASSERT (FALSE);
61     break;
62   }
63 
64   //
65   // something wrong
66   //
67   return 0;
68 }
69 
70 /**
71 
72   Display memory.
73 
74   @param  Address         - Memory Address
75   @param  Count           - Memory Count
76   @param  Width           - Memory Width
77 
78 **/
79 VOID
EdbDisplayMemory(IN UINTN Address,IN UINTN Count,IN EDB_DATA_WIDTH Width)80 EdbDisplayMemory (
81   IN UINTN          Address,
82   IN UINTN          Count,
83   IN EDB_DATA_WIDTH Width
84   )
85 {
86   UINTN  LineNumber;
87   UINTN  ByteNumber;
88   UINTN  LineIndex;
89   UINTN  ByteIndex;
90   UINTN  NumberInLine;
91 
92   if (Count == 0) {
93     return ;
94   }
95 
96   //
97   // Get line number and byte number
98   //
99   switch (Width) {
100   case EdbWidthUint8:
101     NumberInLine = 16;
102     break;
103   case EdbWidthUint16:
104     NumberInLine = 8;
105     break;
106   case EdbWidthUint32:
107     NumberInLine = 4;
108     break;
109   case EdbWidthUint64:
110     NumberInLine = 2;
111     break;
112   default:
113     return;
114   }
115 
116   LineNumber = Count / NumberInLine;
117   ByteNumber = Count % NumberInLine;
118   if (ByteNumber == 0) {
119     LineNumber -= 1;
120     ByteNumber  = NumberInLine;
121   }
122 
123   //
124   // Print each line
125   //
126   for (LineIndex = 0; LineIndex < LineNumber; LineIndex++) {
127 
128     //
129     // Break check
130     //
131     if (((LineIndex % EFI_DEBUGGER_LINE_NUMBER_IN_PAGE) == 0) &&
132         (LineIndex != 0)) {
133       if (SetPageBreak ()) {
134         break;
135       }
136     }
137 
138     EDBPrint (EDB_PRINT_ADDRESS_FORMAT, (UINTN)Address);
139     for (ByteIndex = 0; ByteIndex < NumberInLine; ByteIndex++) {
140       Address += EdbDisplayMemoryUnit (Address, Width);
141     }
142     EDBPrint (L"\n");
143   }
144 
145   //
146   // Break check
147   //
148   if (((LineIndex % EFI_DEBUGGER_LINE_NUMBER_IN_PAGE) == 0) &&
149       (LineIndex != 0)) {
150     if (SetPageBreak ()) {
151       return;
152     }
153   }
154 
155   //
156   // Print last line
157   //
158   EDBPrint (EDB_PRINT_ADDRESS_FORMAT, (UINTN)Address);
159   for (ByteIndex = 0; ByteIndex < ByteNumber; ByteIndex++) {
160     Address += EdbDisplayMemoryUnit (Address, Width);
161   }
162 
163   return ;
164 }
165 
166 /**
167 
168   Entry memory.
169 
170   @param  Address         - Memory Address
171   @param  Value           - Memory Value
172   @param  Width           - Memory Width
173 
174 **/
175 VOID
EdbEnterMemory(IN UINTN Address,IN VOID * Value,IN EDB_DATA_WIDTH Width)176 EdbEnterMemory (
177   IN UINTN          Address,
178   IN VOID           *Value,
179   IN EDB_DATA_WIDTH Width
180   )
181 {
182   switch (Width) {
183   case EdbWidthUint8:
184     CopyMem ((VOID *)Address, Value, sizeof(UINT8));
185     break;
186   case EdbWidthUint16:
187     CopyMem ((VOID *)Address, Value, sizeof(UINT16));
188     break;
189   case EdbWidthUint32:
190     CopyMem ((VOID *)Address, Value, sizeof(UINT32));
191     break;
192   case EdbWidthUint64:
193     CopyMem ((VOID *)Address, Value, sizeof(UINT64));
194     break;
195   default:
196     break;
197   }
198 
199   return ;
200 }
201 
202 /**
203 
204   Get memory address and count.
205 
206   @param  CommandArg      - The argument for this command
207   @param  Address         - Memory Address
208   @param  Count           - Memory Count
209 
210   @retval EFI_SUCCESS           - memory address and count are got
211   @retval EFI_INVALID_PARAMETER - something wrong
212 
213 **/
214 EFI_STATUS
EdbGetMemoryAddressCount(IN CHAR16 * CommandArg,IN UINTN * Address,IN UINTN * Count)215 EdbGetMemoryAddressCount (
216   IN CHAR16    *CommandArg,
217   IN UINTN     *Address,
218   IN UINTN     *Count
219   )
220 {
221   CHAR16       *CommandStr;
222   UINTN        MemAddress;
223   EFI_STATUS   Status;
224 
225   //
226   // Get Address
227   //
228   CommandStr = CommandArg;
229   if (CommandStr == NULL) {
230     EDBPrint (L"Memory: Address error!\n");
231     return EFI_INVALID_PARAMETER;
232   }
233   Status = Symboltoi (CommandStr, &MemAddress);
234   if (EFI_ERROR (Status)) {
235     if (Status == EFI_NOT_FOUND) {
236       MemAddress = Xtoi(CommandStr);
237     } else {
238       //
239       // Something wrong, let Symboltoi print error info.
240       //
241       EDBPrint (L"Command Argument error!\n");
242       return EFI_INVALID_PARAMETER;
243     }
244   }
245   *Address = MemAddress;
246 
247   //
248   // Get Count
249   //
250   CommandStr = StrGetNextTokenLine (L" ");
251   if (CommandStr == NULL) {
252     *Count = 1;
253   } else {
254     *Count = Xtoi(CommandStr);
255   }
256 
257   //
258   // Done
259   //
260   return EFI_SUCCESS;
261 }
262 
263 /**
264 
265   Get memory address and value.
266 
267   @param  CommandArg      - The argument for this command
268   @param  Address         - Memory Address
269   @param  Value           - Memory Value
270 
271   @retval EFI_SUCCESS           - memory address and value are got
272   @retval EFI_INVALID_PARAMETER - something wrong
273 
274 **/
275 EFI_STATUS
EdbGetMemoryAddressValue(IN CHAR16 * CommandArg,IN UINTN * Address,IN UINT64 * Value)276 EdbGetMemoryAddressValue (
277   IN CHAR16    *CommandArg,
278   IN UINTN     *Address,
279   IN UINT64    *Value
280   )
281 {
282   CHAR16       *CommandStr;
283   UINTN        MemAddress;
284   EFI_STATUS   Status;
285 
286   //
287   // Get Address
288   //
289   CommandStr = CommandArg;
290   if (CommandStr == NULL) {
291     EDBPrint (L"Memory: Address error!\n");
292     return EFI_INVALID_PARAMETER;
293   }
294   Status = Symboltoi (CommandStr, &MemAddress);
295   if (EFI_ERROR (Status)) {
296     if (Status == EFI_NOT_FOUND) {
297       MemAddress = Xtoi(CommandStr);
298     } else {
299       //
300       // Something wrong, let Symboltoi print error info.
301       //
302       EDBPrint (L"Command Argument error!\n");
303       return EFI_INVALID_PARAMETER;
304     }
305   }
306   *Address = MemAddress;
307 
308   //
309   // Get Value
310   //
311   CommandStr = StrGetNextTokenLine (L" ");
312   if (CommandStr == NULL) {
313     EDBPrint (L"Memory: Value error!\n");
314     return EFI_INVALID_PARAMETER;
315   }
316   *Value = LXtoi(CommandStr);
317 
318   //
319   // Done
320   //
321   return EFI_SUCCESS;
322 }
323 
324 /**
325 
326   Display memory.
327 
328   @param  CommandArg      - The argument for this command
329   @param  Width           - Memory Width
330 
331   @retval EFI_DEBUG_RETURN   - formal return value
332 
333 **/
334 EFI_DEBUG_STATUS
DebuggerMemoryDisplay(IN CHAR16 * CommandArg,IN EDB_DATA_WIDTH Width)335 DebuggerMemoryDisplay (
336   IN     CHAR16                    *CommandArg,
337   IN     EDB_DATA_WIDTH            Width
338   )
339 {
340   EFI_STATUS Status;
341   UINTN      Address;
342   UINTN      Count;
343 
344   //
345   // Get memory address and count
346   //
347   Status = EdbGetMemoryAddressCount (CommandArg, &Address, &Count);
348   if (EFI_ERROR(Status)) {
349     return EFI_DEBUG_CONTINUE;
350   }
351 
352   //
353   // Display memory
354   //
355   EdbDisplayMemory (Address, Count, Width);
356 
357   //
358   // Done
359   //
360   return EFI_DEBUG_CONTINUE;
361 }
362 
363 /**
364 
365   Enter memory.
366 
367   @param  CommandArg      - The argument for this command
368   @param  Width           - Memory Width
369 
370   @retval EFI_DEBUG_RETURN   - formal return value
371 
372 **/
373 EFI_DEBUG_STATUS
DebuggerMemoryEnter(IN CHAR16 * CommandArg,IN EDB_DATA_WIDTH Width)374 DebuggerMemoryEnter (
375   IN     CHAR16                    *CommandArg,
376   IN     EDB_DATA_WIDTH            Width
377   )
378 {
379   EFI_STATUS Status;
380   UINTN      Address;
381   UINT64     Value;
382 
383   //
384   // Get memory address and value
385   //
386   Status = EdbGetMemoryAddressValue (CommandArg, &Address, &Value);
387   if (EFI_ERROR(Status)) {
388     return EFI_DEBUG_CONTINUE;
389   }
390 
391   //
392   // Enter memory
393   //
394   EdbEnterMemory (Address, &Value, Width);
395 
396   //
397   // Done
398   //
399   return EFI_DEBUG_CONTINUE;
400 }
401 
402 /**
403 
404   DebuggerCommand - DB.
405 
406   @param  CommandArg      - The argument for this command
407   @param  DebuggerPrivate - EBC Debugger private data structure
408   @param  ExceptionType   - Interrupt type.
409   @param  SystemContext   - EBC system context.
410 
411   @retval EFI_DEBUG_RETURN   - formal return value
412 
413 **/
414 EFI_DEBUG_STATUS
DebuggerMemoryDB(IN CHAR16 * CommandArg,IN EFI_DEBUGGER_PRIVATE_DATA * DebuggerPrivate,IN EFI_EXCEPTION_TYPE ExceptionType,IN OUT EFI_SYSTEM_CONTEXT SystemContext)415 DebuggerMemoryDB (
416   IN     CHAR16                    *CommandArg,
417   IN     EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
418   IN     EFI_EXCEPTION_TYPE        ExceptionType,
419   IN OUT EFI_SYSTEM_CONTEXT        SystemContext
420   )
421 {
422   return DebuggerMemoryDisplay (CommandArg, EdbWidthUint8);
423 }
424 
425 /**
426 
427   DebuggerCommand - DW.
428 
429   @param  CommandArg      - The argument for this command
430   @param  DebuggerPrivate - EBC Debugger private data structure
431   @param  ExceptionType   - Interrupt type.
432   @param  SystemContext   - EBC system context.
433 
434   @retval EFI_DEBUG_RETURN   - formal return value
435 
436 **/
437 EFI_DEBUG_STATUS
DebuggerMemoryDW(IN CHAR16 * CommandArg,IN EFI_DEBUGGER_PRIVATE_DATA * DebuggerPrivate,IN EFI_EXCEPTION_TYPE ExceptionType,IN OUT EFI_SYSTEM_CONTEXT SystemContext)438 DebuggerMemoryDW (
439   IN     CHAR16                    *CommandArg,
440   IN     EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
441   IN     EFI_EXCEPTION_TYPE        ExceptionType,
442   IN OUT EFI_SYSTEM_CONTEXT        SystemContext
443   )
444 {
445   return DebuggerMemoryDisplay (CommandArg, EdbWidthUint16);
446 }
447 
448 /**
449 
450   DebuggerCommand - DD.
451 
452   @param  CommandArg      - The argument for this command
453   @param  DebuggerPrivate - EBC Debugger private data structure
454   @param  ExceptionType   - Interrupt type.
455   @param  SystemContext   - EBC system context.
456 
457   @retval EFI_DEBUG_RETURN   - formal return value
458 
459 **/
460 EFI_DEBUG_STATUS
DebuggerMemoryDD(IN CHAR16 * CommandArg,IN EFI_DEBUGGER_PRIVATE_DATA * DebuggerPrivate,IN EFI_EXCEPTION_TYPE ExceptionType,IN OUT EFI_SYSTEM_CONTEXT SystemContext)461 DebuggerMemoryDD (
462   IN     CHAR16                    *CommandArg,
463   IN     EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
464   IN     EFI_EXCEPTION_TYPE        ExceptionType,
465   IN OUT EFI_SYSTEM_CONTEXT        SystemContext
466   )
467 {
468   return DebuggerMemoryDisplay (CommandArg, EdbWidthUint32);
469 }
470 
471 /**
472 
473   DebuggerCommand - DQ.
474 
475   @param  CommandArg      - The argument for this command
476   @param  DebuggerPrivate - EBC Debugger private data structure
477   @param  ExceptionType   - Exception type.
478   @param  SystemContext   - EBC system context.
479 
480   @retval EFI_DEBUG_RETURN   - formal return value
481 
482 **/
483 EFI_DEBUG_STATUS
DebuggerMemoryDQ(IN CHAR16 * CommandArg,IN EFI_DEBUGGER_PRIVATE_DATA * DebuggerPrivate,IN EFI_EXCEPTION_TYPE ExceptionType,IN OUT EFI_SYSTEM_CONTEXT SystemContext)484 DebuggerMemoryDQ (
485   IN     CHAR16                    *CommandArg,
486   IN     EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
487   IN     EFI_EXCEPTION_TYPE        ExceptionType,
488   IN OUT EFI_SYSTEM_CONTEXT        SystemContext
489   )
490 {
491   return DebuggerMemoryDisplay (CommandArg, EdbWidthUint64);
492 }
493 
494 /**
495 
496   DebuggerCommand - EB.
497 
498   @param  CommandArg      - The argument for this command
499   @param  DebuggerPrivate - EBC Debugger private data structure
500   @param  ExceptionType   - Exception type.
501   @param  SystemContext   - EBC system context.
502 
503   @retval EFI_DEBUG_RETURN   - formal return value
504 
505 **/
506 EFI_DEBUG_STATUS
DebuggerMemoryEB(IN CHAR16 * CommandArg,IN EFI_DEBUGGER_PRIVATE_DATA * DebuggerPrivate,IN EFI_EXCEPTION_TYPE ExceptionType,IN OUT EFI_SYSTEM_CONTEXT SystemContext)507 DebuggerMemoryEB (
508   IN     CHAR16                    *CommandArg,
509   IN     EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
510   IN     EFI_EXCEPTION_TYPE        ExceptionType,
511   IN OUT EFI_SYSTEM_CONTEXT        SystemContext
512   )
513 {
514   return DebuggerMemoryEnter (CommandArg, EdbWidthUint8);
515 }
516 
517 /**
518 
519   DebuggerCommand - EW.
520 
521   @param  CommandArg      - The argument for this command
522   @param  DebuggerPrivate - EBC Debugger private data structure
523   @param  ExceptionType   - Interrupt type.
524   @param  SystemContext   - EBC system context.
525 
526   @retval EFI_DEBUG_RETURN   - formal return value
527 
528 **/
529 EFI_DEBUG_STATUS
DebuggerMemoryEW(IN CHAR16 * CommandArg,IN EFI_DEBUGGER_PRIVATE_DATA * DebuggerPrivate,IN EFI_EXCEPTION_TYPE ExceptionType,IN OUT EFI_SYSTEM_CONTEXT SystemContext)530 DebuggerMemoryEW (
531   IN     CHAR16                    *CommandArg,
532   IN     EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
533   IN     EFI_EXCEPTION_TYPE        ExceptionType,
534   IN OUT EFI_SYSTEM_CONTEXT        SystemContext
535   )
536 {
537   return DebuggerMemoryEnter (CommandArg, EdbWidthUint16);
538 }
539 
540 /**
541 
542   DebuggerCommand - ED.
543 
544   @param  CommandArg      - The argument for this command
545   @param  DebuggerPrivate - EBC Debugger private data structure
546   @param  ExceptionType   - Exception type.
547   @param  SystemContext   - EBC system context.
548 
549   @retval EFI_DEBUG_RETURN   - formal return value
550 
551 **/
552 EFI_DEBUG_STATUS
DebuggerMemoryED(IN CHAR16 * CommandArg,IN EFI_DEBUGGER_PRIVATE_DATA * DebuggerPrivate,IN EFI_EXCEPTION_TYPE ExceptionType,IN OUT EFI_SYSTEM_CONTEXT SystemContext)553 DebuggerMemoryED (
554   IN     CHAR16                    *CommandArg,
555   IN     EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
556   IN     EFI_EXCEPTION_TYPE        ExceptionType,
557   IN OUT EFI_SYSTEM_CONTEXT        SystemContext
558   )
559 {
560   return DebuggerMemoryEnter (CommandArg, EdbWidthUint32);
561 }
562 
563 /**
564 
565   DebuggerCommand - EQ.
566 
567   @param  CommandArg      - The argument for this command
568   @param  DebuggerPrivate - EBC Debugger private data structure
569   @param  ExceptionType   - Exception type.
570   @param  SystemContext   - EBC system context.
571 
572   @retval EFI_DEBUG_RETURN   - formal return value
573 
574 **/
575 EFI_DEBUG_STATUS
DebuggerMemoryEQ(IN CHAR16 * CommandArg,IN EFI_DEBUGGER_PRIVATE_DATA * DebuggerPrivate,IN EFI_EXCEPTION_TYPE ExceptionType,IN OUT EFI_SYSTEM_CONTEXT SystemContext)576 DebuggerMemoryEQ (
577   IN     CHAR16                    *CommandArg,
578   IN     EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
579   IN     EFI_EXCEPTION_TYPE        ExceptionType,
580   IN OUT EFI_SYSTEM_CONTEXT        SystemContext
581   )
582 {
583   return DebuggerMemoryEnter (CommandArg, EdbWidthUint64);
584 }
585