Commit 837db3d87f4bfe9261629fb4a1bb433506a3056a

Authored by Mike Frysinger
Committed by Wolfgang Denk
1 parent 18cc7afd9a

tools/envcrc: add --binary option to export embedded env

The --binary option to envcrc can be used to export the embedded env as a
binary blob so that it can be manipulated/examined/whatever externally.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

Showing 1 changed file with 42 additions and 4 deletions Side-by-side Diff

... ... @@ -24,6 +24,7 @@
24 24 #include <stdio.h>
25 25 #include <stdint.h>
26 26 #include <stdlib.h>
  27 +#include <string.h>
27 28 #include <unistd.h>
28 29  
29 30 #ifndef __ASSEMBLY__
30 31  
31 32  
32 33  
... ... @@ -77,19 +78,56 @@
77 78 int main (int argc, char **argv)
78 79 {
79 80 #ifdef ENV_IS_EMBEDDED
  81 + unsigned char pad = 0x00;
80 82 uint32_t crc;
81 83 unsigned char *envptr = &environment,
82 84 *dataptr = envptr + ENV_HEADER_SIZE;
83 85 unsigned int datasize = ENV_SIZE;
  86 + unsigned int eoe;
84 87  
  88 + if (argv[1] && !strncmp(argv[1], "--binary", 8)) {
  89 + int ipad = 0xff;
  90 + if (argv[1][8] == '=')
  91 + sscanf(argv[1] + 9, "%i", &ipad);
  92 + pad = ipad;
  93 + }
  94 +
  95 + if (pad) {
  96 + /* find the end of env */
  97 + for (eoe = 0; eoe < datasize - 1; ++eoe)
  98 + if (!dataptr[eoe] && !dataptr[eoe+1]) {
  99 + eoe += 2;
  100 + break;
  101 + }
  102 + if (eoe < datasize - 1)
  103 + memset(dataptr + eoe, pad, datasize - eoe);
  104 + }
  105 +
85 106 crc = crc32 (0, dataptr, datasize);
86 107  
87 108 /* Check if verbose mode is activated passing a parameter to the program */
88 109 if (argc > 1) {
89   - printf ("CRC32 from offset %08X to %08X of environment = %08X\n",
90   - (unsigned int) (dataptr - envptr),
91   - (unsigned int) (dataptr - envptr) + datasize,
92   - crc);
  110 + if (!strncmp(argv[1], "--binary", 8)) {
  111 + int le = (argc > 2 ? !strcmp(argv[2], "le") : 1);
  112 + size_t i, start, end, step;
  113 + if (le) {
  114 + start = 0;
  115 + end = ENV_HEADER_SIZE;
  116 + step = 1;
  117 + } else {
  118 + start = ENV_HEADER_SIZE - 1;
  119 + end = -1;
  120 + step = -1;
  121 + }
  122 + for (i = start; i != end; i += step)
  123 + printf("%c", (crc & (0xFF << (i * 8))) >> (i * 8));
  124 + fwrite(dataptr, 1, datasize, stdout);
  125 + } else {
  126 + printf("CRC32 from offset %08X to %08X of environment = %08X\n",
  127 + (unsigned int) (dataptr - envptr),
  128 + (unsigned int) (dataptr - envptr) + datasize,
  129 + crc);
  130 + }
93 131 } else {
94 132 printf ("0x%08X\n", crc);
95 133 }