MCPcopy Create free account
hub / github.com/coreboot/coreboot / cmd_add_parse_args

Function cmd_add_parse_args

util/cbfstool/elogtool.c:271–329  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

269}
270
271static int cmd_add_parse_args(uint8_t *type, uint8_t *data, size_t *data_size)
272{
273 char byte[3] = {0};
274 int argc = 0;
275 char *endptr;
276 long value;
277 int len;
278
279 while (cmd_argv[argc] != NULL)
280 argc++;
281
282 if (argc != 1 && argc != 2)
283 return ELOGTOOL_EXIT_BAD_ARGS;
284
285 /* Force type to be an hexa value to be consistent with the data values */
286 value = strtol(cmd_argv[0], NULL, 16);
287 if (value > 255) {
288 fprintf(stderr, "Error: Event type should be between 0-0xff; "
289 "got: 0x%04lx\n", value);
290 return ELOGTOOL_EXIT_BAD_ARGS;
291 }
292
293 *type = value;
294
295 if (argc == 1)
296 return ELOGTOOL_EXIT_SUCCESS;
297
298 /* Assuming argc == 2 */
299 len = strlen(cmd_argv[1]);
300
301 /* Needs 2 bytes per number */
302 if (len % 2 != 0) {
303 fprintf(stderr,
304 "Error: Event data length should be an even number; got: %d\n", len);
305 return ELOGTOOL_EXIT_BAD_ARGS;
306 }
307
308 *data_size = len / 2;
309
310 if (*data_size > ELOG_MAX_EVENT_DATA_SIZE) {
311 fprintf(stderr,
312 "Error: Event data length (in bytes) should be <= %zu; got: %zu\n",
313 ELOG_MAX_EVENT_DATA_SIZE, *data_size);
314 return ELOGTOOL_EXIT_BAD_ARGS;
315 }
316
317 for (unsigned int i = 0; i < *data_size; i++) {
318 byte[0] = *cmd_argv[1]++;
319 byte[1] = *cmd_argv[1]++;
320 data[i] = strtol(byte, &endptr, 16);
321 if (endptr != &byte[2]) {
322 fprintf(stderr, "Error: Event data length contains invalid data. "
323 "Only hexa digits are valid\n");
324 return ELOGTOOL_EXIT_BAD_ARGS;
325 }
326 }
327
328 return ELOGTOOL_EXIT_SUCCESS;

Callers 1

cmd_addFunction · 0.85

Calls 3

fprintfFunction · 0.85
strlenFunction · 0.85
strtolFunction · 0.50

Tested by

no test coverage detected