> Date: Tue, 26 Nov 2019 23:22:14 +0100
> From: Alexander Bluhm <[email protected]>
> 
> Hi,
> 
> I am currently debugging an UEFI boot problem.  A hexdump in the
> boot loader would be convenient.
> 
> boot> hexdump 0x10 20
> 00000010  53 ff 00 f0 54 ff 00 f0  53 ff 00 f0 53 ff 00 f0  |S...T...S...S...|
> 00000020  a5 fe 00 f0                                       |....|
> 00000024
> 
> Do we want such a feature?

Sounds useful to me.

> If so, I would adapt the other's architectures Makefiles.
> 
> bluhm
> 
> Index: arch/amd64/stand/boot/Makefile
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/sys/arch/amd64/stand/boot/Makefile,v
> retrieving revision 1.43
> diff -u -p -r1.43 Makefile
> --- arch/amd64/stand/boot/Makefile    29 Oct 2019 02:55:50 -0000      1.43
> +++ arch/amd64/stand/boot/Makefile    26 Nov 2019 22:19:50 -0000
> @@ -32,7 +32,8 @@ SRCS+=      softraid_amd64.c
>  .endif
> 
>  .PATH:       ${S}/lib/libsa
> -SRCS+=       alloc.c ctime.c exit.c getchar.c memcmp.c memcpy.c memmove.c 
> memset.c printf.c \
> +SRCS+=       alloc.c ctime.c exit.c hexdump.c getchar.c \
> +     memcmp.c memcpy.c memmove.c memset.c printf.c \
>       putchar.c snprintf.c strcmp.c strerror.c strlen.c strncmp.c strncpy.c \
>       strtol.c strtoll.c
>  SRCS+=       close.c closeall.c cons.c cread.c dev.c disklabel.c dkcksum.c 
> fchmod.c \
> Index: arch/amd64/stand/efiboot/Makefile.common
> ===================================================================
> RCS file: 
> /data/mirror/openbsd/cvs/src/sys/arch/amd64/stand/efiboot/Makefile.common,v
> retrieving revision 1.18
> diff -u -p -r1.18 Makefile.common
> --- arch/amd64/stand/efiboot/Makefile.common  29 Oct 2019 02:55:51 -0000      
> 1.18
> +++ arch/amd64/stand/efiboot/Makefile.common  26 Nov 2019 21:57:05 -0000
> @@ -32,7 +32,8 @@ SRCS+=      memprobe.c
>  SRCS+=       boot.c bootarg.c cmd.c vars.c
> 
>  .PATH:       ${S}/lib/libsa
> -SRCS+=       alloc.c ctime.c exit.c getchar.c memcmp.c memcpy.c memmove.c 
> memset.c printf.c \
> +SRCS+=       alloc.c ctime.c exit.c hexdump.c getchar.c \
> +     memcmp.c memcpy.c memmove.c memset.c printf.c \
>       putchar.c snprintf.c strcmp.c strerror.c strlen.c strncmp.c strncpy.c \
>       strtol.c strtoll.c
>  SRCS+=       close.c closeall.c cons.c cread.c dev.c disklabel.c dkcksum.c \
> Index: lib/libsa/hexdump.c
> ===================================================================
> RCS file: lib/libsa/hexdump.c
> diff -N lib/libsa/hexdump.c
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ lib/libsa/hexdump.c       26 Nov 2019 22:17:53 -0000
> @@ -0,0 +1,52 @@
> +/*   $OpenBSD$       */
> +/*
> + * Copyright (c) 2019 Alexander Bluhm <[email protected]>
> + *
> + * Permission to use, copy, modify, and distribute this software for any
> + * purpose with or without fee is hereby granted, provided that the above
> + * copyright notice and this permission notice appear in all copies.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
> + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
> + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
> + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
> + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
> + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
> + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
> + */
> +
> +#include <sys/types.h>
> +
> +#include "stand.h"
> +
> +void
> +hexdump(const void *addr, size_t size)
> +{
> +     const unsigned char *line, *end;
> +     int byte;
> +
> +     end = (const char *)addr + size;
> +     for (line = addr; line < end; line += 16) {
> +             printf("%08x  ", line);
> +             for (byte = 0; byte < 16; byte++) {
> +                     if (&line[byte] < end)
> +                             printf("%02x ", line[byte]);
> +                     else
> +                             printf("   ");
> +                     if (byte == 7)
> +                             printf(" ");
> +             }
> +             printf(" |");
> +             for (byte = 0; byte < 16; byte++) {
> +                     if (&line[byte] < end) {
> +                             if (line[byte] >= ' ' && line[byte] <= '~')
> +                                     printf("%c", line[byte]);
> +                             else
> +                                     printf(".");
> +                     } else
> +                             break;
> +             }
> +             printf("|\n");
> +     }
> +     printf("%08x\n", end);
> +}
> Index: lib/libsa/stand.h
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/sys/lib/libsa/stand.h,v
> retrieving revision 1.68
> diff -u -p -r1.68 stand.h
> --- lib/libsa/stand.h 1 Nov 2019 20:54:52 -0000       1.68
> +++ lib/libsa/stand.h 26 Nov 2019 21:57:05 -0000
> @@ -146,6 +146,7 @@ __dead void       _rtt(void) __attribute__((no
>  #define bcmp(s1,s2,n)        (memcmp((s2),(s1),(n)))
>  #define      bcopy(s1,s2,n)  ((void)memmove((s2),(s1),(n)))
>  void explicit_bzero(void *, size_t);
> +void hexdump(const void *, size_t);
>  void *memcpy(void *, const void *, size_t);
>  void *memmove(void *, const void *, size_t);
>  int  memcmp(const void *, const void *, size_t);
> Index: stand/boot/cmd.c
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/sys/stand/boot/cmd.c,v
> retrieving revision 1.65
> diff -u -p -r1.65 cmd.c
> --- stand/boot/cmd.c  3 Aug 2019 15:22:19 -0000       1.65
> +++ stand/boot/cmd.c  26 Nov 2019 21:57:05 -0000
> @@ -39,6 +39,7 @@
>  static int Xboot(void);
>  static int Xecho(void);
>  static int Xhelp(void);
> +static int Xhexdump(void);
>  static int Xls(void);
>  static int Xnop(void);
>  static int Xreboot(void);
> @@ -62,6 +63,7 @@ const struct cmd_table cmd_table[] = {
>       {"echo",   CMDT_CMD, Xecho},
>       {"env",    CMDT_CMD, Xenv},
>       {"help",   CMDT_CMD, Xhelp},
> +     {"hexdump",CMDT_CMD, Xhexdump},
>       {"ls",     CMDT_CMD, Xls},
>  #ifdef MACHINE_CMD
>       {"machine",CMDT_MDC, Xmachine},
> @@ -345,6 +347,51 @@ Xhelp(void)
>  #else
>       return 0;
>  #endif
> +}
> +
> +static int
> +Xhexdump(void)
> +{
> +     unsigned long val[2];
> +     const unsigned char *p;
> +     unsigned int n, b, d;
> +
> +     if (cmd.argc != 3) {
> +             printf("hexdump addr size\n");
> +             return 0;
> +     }
> +
> +     for (n = 1; n < cmd.argc; n++) {
> +             p = cmd.argv[n];
> +             if (*p == '0') {
> +                     p++;
> +                     if (*p == 'x' || *p == 'X') {
> +                             p++;
> +                             b = 16;
> +                     } else
> +                             b = 8;
> +             } else
> +                     b = 10;
> +             val[n-1] = 0;
> +             for (; *p != '\0'; p++) {
> +                     if (*p >= '0' && *p <= '9')
> +                             d = *p - '0';
> +                     else if (*p >= 'a' && *p <= 'z')
> +                             d = *p - 'a' + 10;
> +                     else if (*p >= 'A' && *p <= 'Z')
> +                             d = *p - 'A' + 10;
> +                     else
> +                             goto err;
> +                     if (d >= b)
> +                             goto err;
> +                     val[n-1] = val[n-1] * b + d;
> +             }
> +     }
> +     hexdump((void *)val[0], val[1]);
> +     return 0;
> + err:
> +     printf("bad '%c' in \"%s\"\n", *p, cmd.argv[n]);
> +     return 0;
>  }
> 
>  #ifdef MACHINE_CMD
> 
> 

Reply via email to