• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/firmware-utils/src/nosimg-enc.c

  1 // SPDX-License-Identifier: GPL-2.0-or-later
  2 /*
  3  * nosimg-enc.c - encode/decode "nos.img" image for XikeStor SKS8300 series
  4  */
  5 
  6 #include <stdbool.h>
  7 #include <stdint.h>
  8 #include <stdio.h>
  9 #include <stdlib.h>
 10 #include <string.h>
 11 #include <unistd.h>
 12 
 13 #define ENCODE_BLKLEN   0x100
 14 #define ENCODE_BLOCKS   2
 15 
 16 static const uint8_t key[ENCODE_BLKLEN] = {
 17         0xee, 0xdd, 0xcc, 0x21, 0x53, 0x55, 0xee, 0xcc,
 18         0xdd, 0x55, 0x80, 0x7e, 0x00, 0x00, 0x00, 0x00,
 19         0xcd, 0xbd, 0xdf, 0xae, 0xbb, 0x9b, 0x89, 0x01,
 20         0x70, 0xe5, 0xcc, 0xdd, 0xf6, 0xfc, 0x83, 0x64,
 21         0xec, 0xdd, 0xce, 0xf1, 0xe3, 0x54, 0xfe, 0xd0,
 22         0xbd, 0xab, 0xdd, 0xe1, 0xe4, 0xb4, 0xd5, 0x83,
 23         0xed, 0xfe, 0xd0, 0xcd, 0xb6, 0x55, 0xcc, 0xa3,
 24         0xed, 0xd5, 0xc6, 0x7e, 0xdd, 0xcc, 0x21, 0x53,
 25         0xec, 0x4d, 0xdc, 0x00, 0x53, 0x55, 0xcd, 0xc3,
 26         0x22, 0x01, 0x80, 0x7e, 0xef, 0xbc, 0x75, 0x66,
 27         0xa6, 0xc0, 0xcc, 0x2f, 0xfe, 0xd0, 0xee, 0xcc,
 28         0xdd, 0x55, 0x01, 0x01, 0x01, 0x01, 0xc5, 0x64,
 29         0x99, 0x45, 0xab, 0x32, 0x55, 0x80, 0x7e, 0xef,
 30         0x55, 0x80, 0x7e, 0xef, 0xbc, 0x75, 0x66, 0x89,
 31         0xe3, 0x1d, 0x83, 0xdd, 0xfe, 0x55, 0x8e, 0xab,
 32         0x7d, 0x55, 0x80, 0x7e, 0xff, 0x01, 0xac, 0x66,
 33         0x0e, 0xc9, 0x92, 0xd9, 0x73, 0xe5, 0x01, 0x01,
 34         0xbd, 0xe5, 0x10, 0xce, 0x01, 0x01, 0xba, 0xe8,
 35         0x3e, 0xdd, 0x81, 0xa1, 0x53, 0x33, 0x01, 0x01,
 36         0x9a, 0xc5, 0x10, 0xaa, 0x01, 0xce, 0x8a, 0xe1,
 37         0xb1, 0xfb, 0x00, 0x80, 0x53, 0x77, 0x00, 0x00,
 38         0x70, 0xdc, 0x00, 0x01, 0x00, 0x00, 0xcb, 0xb1,
 39         0xa0, 0x30, 0x00, 0x00, 0x55, 0xa6, 0x00, 0x00,
 40         0xca, 0xbd, 0x01, 0x01, 0x00, 0x00, 0xc9, 0xb2,
 41         0x81, 0x90, 0x01, 0x00, 0x5a, 0x21, 0x00, 0x01,
 42         0x79, 0xbc, 0x01, 0x00, 0x78, 0x00, 0x7b, 0xb3,
 43         0xd4, 0x97, 0x01, 0x00, 0x53, 0x55, 0xa9, 0xfc,
 44         0xdd, 0xa5, 0x01, 0xbe, 0xaf, 0xc1, 0x75, 0xc5,
 45         0x8e, 0xd7, 0x77, 0x00, 0x55, 0xd0, 0x0d, 0xac,
 46         0x01, 0x55, 0x80, 0x7e, 0xef, 0xbc, 0x7e, 0xe6,
 47         0xf1, 0x6c, 0x52, 0x00, 0x33, 0x16, 0x98, 0xcc,
 48         0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x79, 0x88,
 49 };
 50 
 51 static void __attribute__((noreturn)) usage(void)
 52 {
 53         fprintf(stderr, "Usage: nosimg-enc -i infile -o outfile [-d]\n");
 54         exit(EXIT_FAILURE);
 55 }
 56 
 57 static void encode_block(uint8_t *data, bool decode)
 58 {
 59         int i;
 60 
 61         for (i = 0; i < ENCODE_BLKLEN; i++)
 62                 data[i] -= key[i] * (decode ? -1 : 1);
 63 }
 64 
 65 int main(int argc, char **argv)
 66 {
 67         int i, c, n, ret = EXIT_SUCCESS;
 68         char *ifn = NULL, *ofn = NULL;
 69         bool decode = false;
 70         uint8_t buf[0x1000];
 71         FILE *out, *in;
 72 
 73         while ((c = getopt(argc, argv, "i:o:dh")) != -1) {
 74                 switch (c) {
 75                 case 'i':
 76                         ifn = optarg;
 77                         break;
 78                 case 'o':
 79                         ofn = optarg;
 80                         break;
 81                 case 'd':
 82                         decode = true;
 83                         break;
 84                 case 'h':
 85                 default:
 86                         usage();
 87                 }
 88         }
 89 
 90         if (optind != argc || optind == 1) {
 91                 fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
 92                 usage();
 93         }
 94 
 95         in = fopen(ifn, "r");
 96         if (!in) {
 97                 perror("can not open input file");
 98                 usage();
 99         }
100 
101         out = fopen(ofn, "w");
102         if (!out) {
103                 perror("can not open output file");
104                 usage();
105         }
106 
107         /* encode/decode the first 512 bytes (0x100 x2) */
108         for (i = 0; i < ENCODE_BLOCKS; i++) {
109                 n = fread(buf, 1, ENCODE_BLKLEN, in);
110                 if (n < ENCODE_BLKLEN) {
111                         fprintf(stderr,
112                                 "failed to read data for encoding/decoding\n");
113                         ret = EXIT_FAILURE;
114                         goto out;
115                 }
116 
117                 encode_block(buf, decode);
118                 fwrite(buf, 1, ENCODE_BLKLEN, out);
119         }
120 
121         /* copy the remaining data */
122         while ((n = fread(buf, 1, sizeof(buf), in)) > 0) {
123                 if (fwrite(buf, 1, n, out) != n) {
124                         fprintf(stderr, "failed to write");
125                         ret = EXIT_FAILURE;
126                         goto out;
127                 }
128         }
129 
130         if (ferror(in)) {
131                 perror("failed to read");
132                 ret = EXIT_FAILURE;
133         }
134 
135 out:
136         fclose(in);
137         fclose(out);
138         return ret;
139 }
140 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt