summaryrefslogtreecommitdiffstats
path: root/common/recipes-core/fruid/files/fruid-util.c
blob: d0e19b88463e26686abb4bebece9dd95b32d332f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright 2015-present Facebook. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <syslog.h>
#include <string.h>
#include <openbmc/fruid.h>
#include <openbmc/pal.h>

#define EEPROM_READ     0x1
#define EEPROM_WRITE    0x2
#define FRUID_SIZE      256

/* To copy the bin files */
static int
copy_file(int out, int in, int bs) {

  ssize_t bytes_rd, bytes_wr;
  uint64_t tmp[FRUID_SIZE];

  while ((bytes_rd = read(in, tmp, FRUID_SIZE)) > 0) {
    bytes_wr = write(out, tmp, bytes_rd);
    if (bytes_wr != bytes_rd) {
      return errno;
    }
  }
  return 0;
}

/* Print the FRUID in detail */
static void
print_fruid_info(fruid_info_t *fruid, const char *name)
{
  /* Print format */
  printf("%-27s: %s", "\nFRU Information",
      name /* Name of the FRU device */ );
  printf("%-27s: %s", "\n---------------", "------------------");

  if (fruid->chassis.flag) {
    printf("%-27s: %s", "\nChassis Type",fruid->chassis.type_str);
    printf("%-27s: %s", "\nChassis Part",fruid->chassis.part);
    printf("%-27s: %s", "\nChassis Serial Number",fruid->chassis.serial);
    printf("%-27s: %s", "\nChassis Custom Data",fruid->chassis.custom);
  }

  if (fruid->board.flag) {
    printf("%-27s: %s", "\nBoard Mfg Time",fruid->board.mfg_time_str);
    printf("%-27s: %s", "\nBoard Manufacturer",fruid->board.mfg);
    printf("%-27s: %s", "\nBoard Name",fruid->board.name);
    printf("%-27s: %s", "\nBoard Serial Number",fruid->board.serial);
    printf("%-27s: %s", "\nBoard Part",fruid->board.part);
    printf("%-27s: %s", "\nBoard FRU ID",fruid->board.fruid);
    printf("%-27s: %s", "\nBoard Custom Data",fruid->board.custom);
  }

  if (fruid->product.flag) {
    printf("%-27s: %s", "\nProduct Manufacturer",fruid->product.mfg);
    printf("%-27s: %s", "\nProduct Name",fruid->product.name);
    printf("%-27s: %s", "\nProduct Part",fruid->product.part);
    printf("%-27s: %s", "\nProduct Version",fruid->product.version);
    printf("%-27s: %s", "\nProduct Serial Number",fruid->product.serial);
    printf("%-27s: %s", "\nProduct Asset Tag",fruid->product.asset_tag);
    printf("%-27s: %s", "\nProduct FRU ID",fruid->product.fruid);
    printf("%-27s: %s", "\nProduct Custom Data",fruid->product.custom);
  }

  printf("\n");
}

/* Populate and print fruid_info by parsing the fru's binary dump */
void get_fruid_info(uint8_t fru, char *path, char* name) {
  int ret;
  fruid_info_t fruid;

  ret = fruid_parse(path, &fruid);
  if (ret) {
    fprintf(stderr, "Failed print FRUID for %s\nCheck syslog for errors!\n",
        name);
  } else {
    print_fruid_info(&fruid, name);
    free_fruid_info(&fruid);
  }

}

static int
print_usage() {
  printf("Usage: fruid-util [ %s ]\n"
      "Usage: fruid-util [%s] [--dump | --write ] <file>\n",
      pal_fru_list, pal_fru_list);
}

/* Utility to just print the FRUID */
int main(int argc, char * argv[]) {

  int ret;
  int rw = 0;
  int fd_tmpbin;
  int fd_newbin;
  int fd_eeprom;
  uint8_t fru;
  char *file_path = NULL;
  char path[64] = {0};
  char eeprom_path[64] = {0};
  char name[64] = {0};
  char command[128] = {0};

  if (argc != 2 && argc != 4) {
    print_usage();
    exit(-1);
  }

  ret = pal_get_fru_id(argv[1], &fru);
  if (ret < 0) {
    print_usage();
    return ret;
  }

  if (fru == 0 && argc > 2) {
    print_usage();
    exit(-1);
  }

  if (argc > 2) {
    if (!strcmp(argv[2], "--dump")) {
      rw = EEPROM_READ;
      file_path = argv[3];
    } else if (!strcmp(argv[2], "--write")) {
      rw = EEPROM_WRITE;
      file_path = argv[3];
    }
  }

  // Check if the new eeprom binary file exits.
  // TODO: Add file size check before adding to the eeprom
  if (rw == EEPROM_WRITE && (access(file_path, F_OK) == -1)) {
      print_usage();
      exit(-1);
  }

  if (fru != 0) {
    ret = pal_get_fruid_path(fru, path);
    if (ret < 0) {
      return ret;
    }

    errno = 0;

    /* FRUID BINARY DUMP */
    if (rw == EEPROM_READ) {
      fd_tmpbin = open(path, O_RDONLY);
      if (fd_tmpbin == -1) {
        syslog(LOG_ERR, "Unable to open the %s file: %s", path, strerror(errno));
        return errno;
      }

      fd_newbin = open(file_path, O_WRONLY | O_CREAT, 0644);
      if (fd_newbin == -1) {
        syslog(LOG_ERR, "Unable to create %s file: %s", file_path, strerror(errno));
        return errno;
      }

      ret = copy_file(fd_newbin, fd_tmpbin, FRUID_SIZE);
      if (ret < 0) {
        syslog(LOG_ERR, "copy: write to %s file failed: %s",
            file_path, strerror(errno));
        return ret;
      }

      close(fd_newbin);
      close(fd_tmpbin);

    } else if (rw == EEPROM_WRITE) {

    /* FRUID BINARY WRITE */

      fd_tmpbin = open(path, O_WRONLY);
      if (fd_tmpbin == -1) {
        syslog(LOG_ERR, "Unable to open the %s file: %s", path, strerror(errno));
        return errno;
      }

      fd_newbin = open(file_path, O_RDONLY);
      if (fd_newbin == -1) {
        syslog(LOG_ERR, "Unable to open the %s file: %s", file_path, strerror(errno));
        return errno;
      }

      ret = pal_get_fruid_eeprom_path(fru, eeprom_path);
      if (ret < 0) {
        //Can not handle in common, so call pal libray for update
        pal_fruid_write(fru, file_path);
      } else {
        if (access(eeprom_path, F_OK) == -1) {
          syslog(LOG_ERR, "cannot access the eeprom file : %s for fru %d",
              eeprom_path, fru);
          close(fd_newbin);
          close(fd_tmpbin);
          return -1;
        }
        sprintf(command, "dd if=%s of=%s bs=%d count=1", file_path, eeprom_path, FRUID_SIZE);
        system(command);
      }

      ret = copy_file(fd_tmpbin, fd_newbin, FRUID_SIZE);
      if (ret < 0) {
        syslog(LOG_ERR, "copy: write to %s file failed: %s",
            path, strerror(errno));
        return ret;
      }

      close(fd_newbin);
      close(fd_tmpbin);

    } else {
      /* FRUID PRINT ONE FRU */

      ret = pal_get_fruid_name(fru, name);
      if (ret < 0) {
        return ret;
      }

      get_fruid_info(fru, path, name);
    }

  } else if (fru == 0) {

    /* FRUID PRINT ALL FRUs */

    fru = 1;

    while (fru <= MAX_NUM_FRUS) {
      ret = pal_get_fruid_path(fru, path);
      if (ret < 0) {
        return ret;
      }

      ret = pal_get_fruid_name(fru, name);
      if (ret < 0) {
        return ret;
      }

      get_fruid_info(fru, path, name);

      fru++;
    }
  }

  return 0;
}
OpenPOWER on IntegriCloud