Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /**
0002  * @file
0003  * 
0004  * @brief MDUMP Shell Command Implmentation
0005  */
0006 
0007 /*
0008  * Copyright (c) 2001 Fernando Ruiz Casas <fruizcasas@gmail.com>
0009  *
0010  *  Reworked by Ric Claus at SLAC.Stanford.edu
0011  *
0012  *  The license and distribution terms for this file may be
0013  *  found in the file LICENSE in this distribution or at
0014  *  http://www.rtems.org/license/LICENSE.
0015  */
0016 
0017 #ifdef HAVE_CONFIG_H
0018 #include "config.h"
0019 #endif
0020 
0021 #include <ctype.h>
0022 #include <stdio.h>
0023 #include <inttypes.h>
0024 #include <string.h>
0025 
0026 #include <rtems.h>
0027 #include <rtems/shell.h>
0028 #include <rtems/stringto.h>
0029 #include "internal.h"
0030 
0031 static int args_parse(int argc, char* argv[], void** addr, int* max, int* sz);
0032 
0033 static void mdumpB(void* addr, int m);
0034 static void mdumpW(void* addr, int m);
0035 static void mdumpL(void* addr, int m);
0036 static void mdumpC(void* addr, int m);
0037 
0038 int rtems_mdump(void* addr, int max, int sz);
0039 
0040 static int rtems_shell_main_mdump(
0041   int   argc,
0042   char *argv[]
0043 )
0044 {
0045   void *addr;
0046   int   max;
0047   int   sz;
0048 
0049   if (args_parse(argc, argv, &addr, &max, &sz))
0050     return -1;
0051   return rtems_mdump(addr, max, sz);
0052 }
0053 
0054 static int rtems_shell_main_wdump(
0055   int   argc,
0056   char *argv[]
0057 )
0058 {
0059   void *addr;
0060   int   max;
0061   int   sz;
0062 
0063   if (args_parse(argc, argv, &addr, &max, &sz))
0064     return -1;
0065   return rtems_mdump(addr, max, 2);
0066 }
0067 
0068 
0069 static int rtems_shell_main_ldump(
0070   int   argc,
0071   char *argv[]
0072 )
0073 {
0074   void *addr;
0075   int   max;
0076   int   sz;
0077 
0078   if (args_parse(argc, argv, &addr, &max, &sz))
0079     return -1;
0080   return rtems_mdump(addr, max, 4);
0081 }
0082 
0083 
0084 int args_parse(int    argc,
0085                char*  argv[],
0086                void** addr,
0087                int*   max,
0088                int*   sz)
0089 {
0090   *addr = NULL;
0091   *max  = 320;
0092   *sz   = 1;
0093 
0094   if (argc > 1) {
0095     if ( rtems_string_to_pointer(argv[1], addr, NULL) ) {
0096       printf( "Address argument (%s) is not a number\n", argv[1] );
0097       return -1;
0098     }
0099 
0100     if (argc > 2) {
0101       if ( rtems_string_to_int(argv[2], max, NULL, 0) ) {
0102         printf( "Length argument (%s) is not a number\n", argv[2] );
0103         return -1;
0104       }
0105 
0106       if (argc > 3) {
0107         if ( rtems_string_to_int(argv[3], sz, NULL, 0) ) {
0108           printf( "Size argument (%s) is not a valid number\n", argv[3] );
0109           return -1;
0110         }
0111       }
0112     }
0113   }
0114   return 0;
0115 }
0116 
0117 
0118 int rtems_mdump(void* addr, int max, int sz)
0119 {
0120   unsigned char  m;
0121   unsigned char *pb;
0122   int            res;
0123   int            cnt;
0124 
0125   if (!((sz == 1) || (sz == 2) || (sz == 4))) {
0126     printf( "Size argument (%d) is not one of 1 (bytes), "
0127               " 2 (words) or 4 (longwords)\n", sz);
0128     return -1;
0129   }
0130 
0131   if (max <= 0) {
0132     max = 1;           /* print 1 item if 0 or neg. */
0133     res = 0;
0134   } else {
0135     max--;
0136     res = max & 0xf;   /* num bytes in last row */
0137     max >>= 4;         /* div by 16 */
0138     max++;             /* num of rows to print */
0139     if (max > 64) {    /* limit to 64 lines */
0140       max = 64;
0141       res = 0xf;       /* 16 bytes print in last row */
0142     }
0143   }
0144 
0145   pb = addr;
0146   for (m=0; m<max; m++) {
0147     cnt = m==(max-1)?res:0xf;
0148     printf("%10p ", pb);
0149     if      (sz == 1)  mdumpB(pb, cnt);
0150     else if (sz == 2)  mdumpW(pb, cnt);
0151     else if (sz == 4)  mdumpL(pb, cnt);
0152     mdumpC(pb, cnt);
0153     printf("\n");
0154     pb += 16;
0155   }
0156 
0157   return 0;
0158 }
0159 
0160 
0161 void mdumpB(void* addr, int m)
0162 {
0163   volatile unsigned char* pb = addr;
0164   int n;
0165   for (n=0;n<=m;n++)
0166     printf("%02X%c",*pb++,n==7?'-':' ');
0167   for (;n<=0xf;n++)
0168     printf("  %c",n==7?'-':' ');
0169 }
0170 
0171 
0172 void mdumpW(void* addr, int m)
0173 {
0174   volatile unsigned short* pb = addr;
0175   int n;
0176   for (n=0;n<=m;n+=2)
0177     printf("%04X%c",*pb++,n==6?'-':' ');
0178   for (;n<=0xf;n+=2)
0179     printf("    %c", n==6?'-':' ');
0180 }
0181 
0182 
0183 void mdumpL(void* addr, int m)
0184 {
0185   volatile unsigned int* pb = addr;
0186   int n;
0187   for (n=0;n<=m;n+=4)
0188     printf("%08X%c",*pb++,n==4?'-':' ');
0189   for (;n<=0xf;n+=4)
0190     printf("        %c", n==4?'-':' ');
0191 }
0192 
0193 
0194 void mdumpC(void* addr, int m)
0195 {
0196   volatile unsigned char* pb = addr;
0197   int n;
0198   for (n=0;n<=m;n++)
0199     printf("%c", isprint(pb[n]) ? pb[n] : '.');
0200 }
0201 
0202 
0203 rtems_shell_cmd_t rtems_shell_MDUMP_Command = {
0204   "mdump",                                      /* name */
0205   "mdump [address [length [size]]]",            /* usage */
0206   "mem",                                        /* topic */
0207   rtems_shell_main_mdump,                       /* command */
0208   NULL,                                         /* alias */
0209   NULL                                          /* next */
0210 };
0211 
0212 
0213 rtems_shell_cmd_t rtems_shell_WDUMP_Command = {
0214   "wdump",                                      /* name */
0215   "wdump [address [length]]",                   /* usage */
0216   "mem",                                        /* topic */
0217   rtems_shell_main_wdump,                       /* command */
0218   NULL,                                         /* alias */
0219   NULL                                          /* next */
0220 };
0221 
0222 
0223 rtems_shell_cmd_t rtems_shell_LDUMP_Command = {
0224   "ldump",                                      /* name */
0225   "ldump [address [length]]",                   /* usage */
0226   "mem",                                        /* topic */
0227   rtems_shell_main_ldump,                       /* command */
0228   NULL,                                         /* alias */
0229   NULL                                          /* next */
0230 };
0231