Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 1 | /*****************************************************************************\ |
| 2 | * common.c |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 3 | ***************************************************************************** |
| 4 | * Copyright (C) 2002-2005 The Regents of the University of California. |
| 5 | * Produced at the Lawrence Livermore National Laboratory. |
| 6 | * Written by Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>. |
| 7 | * UCRL-CODE-2003-012 |
| 8 | * All rights reserved. |
| 9 | * |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 10 | * This file is part of nvramtool, a utility for reading/writing coreboot |
Stefan Reinauer | f527e70 | 2008-01-18 15:33:49 +0000 | [diff] [blame] | 11 | * parameters and displaying information from the coreboot table. |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 12 | * For details, see http://coreboot.org/nvramtool. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 13 | * |
| 14 | * Please also read the file DISCLAIMER which is included in this software |
| 15 | * distribution. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or modify it |
| 18 | * under the terms of the GNU General Public License (as published by the |
| 19 | * Free Software Foundation) version 2, dated June 1991. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, but |
| 22 | * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and |
| 24 | * conditions of the GNU General Public License for more details. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 25 | \*****************************************************************************/ |
| 26 | |
| 27 | #include "common.h" |
| 28 | |
| 29 | /* basename of this program, as reported by argv[0] */ |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 30 | const char prog_name[] = "nvramtool"; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 31 | |
| 32 | /* version of this program */ |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 33 | const char prog_version[] = "2.1"; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 34 | |
| 35 | /**************************************************************************** |
| 36 | * get_line_from_file |
| 37 | * |
| 38 | * Get a line of input from file 'f'. Store result in 'line' which is an |
| 39 | * array of 'line_buf_size' bytes. |
| 40 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 41 | int get_line_from_file(FILE * f, char line[], int line_buf_size) |
| 42 | { |
| 43 | if (fgets(line, line_buf_size, f) == NULL) |
| 44 | return LINE_EOF; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 45 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 46 | /* If the file contains a line that is too long, then it's best |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 47 | * to let the user know right away rather than passing back a |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 48 | * truncated result that will lead to problems later on. |
| 49 | */ |
| 50 | return (strlen(line) == ((size_t) (line_buf_size - 1))) ? |
| 51 | LINE_TOO_LONG : OK; |
| 52 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 53 | |
| 54 | /**************************************************************************** |
| 55 | * out_of_memory |
| 56 | * |
| 57 | * We ran out of memory. Print an error message and die. |
| 58 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 59 | void out_of_memory(void) |
| 60 | { |
| 61 | fprintf(stderr, "%s: Out of memory.\n", prog_name); |
| 62 | exit(1); |
| 63 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 64 | |
| 65 | /**************************************************************************** |
| 66 | * usage |
| 67 | * |
| 68 | * Write a usage message to 'outfile'. If 'outfile' is 'stderr' then exit |
| 69 | * with a value of 1. Otherwise exit with a value of 0. |
| 70 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 71 | void usage(FILE * outfile) |
| 72 | { |
| 73 | fprintf(outfile, |
| 74 | "Usage: %s [-y LAYOUT_FILE | -t] PARAMETER ...\n\n" |
| 75 | " Read/write coreboot parameters or show info from " |
| 76 | "coreboot table.\n\n" |
| 77 | " -y LAYOUT_FILE: Use CMOS layout specified by " |
| 78 | "LAYOUT_FILE.\n" |
| 79 | " -t: Use CMOS layout specified by CMOS option " |
| 80 | "table.\n" |
Patrick Georgi | 202be7b | 2011-01-21 07:29:40 +0000 | [diff] [blame] | 81 | " -C CBFS_FILE: Use CBFS file for layout and CMOS data.\n" |
| 82 | " -D CMOS_FILE: Use CMOS file for CMOS data (overrides CMOS of -C).\n" |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 83 | " [-n] -r NAME: Show parameter NAME. If -n is given, " |
| 84 | "show value only.\n" |
| 85 | " -e NAME: Show all possible values for parameter " |
| 86 | "NAME.\n" |
| 87 | " -a: Show names and values for all " |
| 88 | "parameters.\n" |
| 89 | " -w NAME=VALUE: Set parameter NAME to VALUE.\n" |
| 90 | " -p INPUT_FILE: Set parameters according to INPUT_FILE.\n" |
| 91 | " -i: Same as -p but file contents taken from " |
| 92 | "standard input.\n" |
| 93 | " -c [VALUE]: Show CMOS checksum or set checksum to " |
| 94 | "VALUE.\n" |
| 95 | " -l [ARG]: Show coreboot table info for ARG, or " |
| 96 | "all ARG choices.\n" |
Vikram Narayanan | a8111cf | 2012-04-14 15:25:13 +0530 | [diff] [blame] | 97 | " -L OUTPUT_BIN Write CMOS layout file in binary format\n" |
| 98 | " -H OUTPUT_HDR Write CMOS layout file in header format\n" |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 99 | " -d: Show low-level dump of coreboot table.\n" |
| 100 | " -Y: Show CMOS layout info.\n" |
| 101 | " -b OUTPUT_FILE: Dump CMOS memory contents to file.\n" |
| 102 | " -B INPUT_FILE: Write file contents to CMOS memory.\n" |
| 103 | " -x: Show hex dump of CMOS memory.\n" |
| 104 | " -X DUMPFILE: Show hex dump of CMOS dumpfile.\n" |
| 105 | " -v: Show version info for this program.\n" |
| 106 | " -h: Show this message.\n", prog_name); |
| 107 | exit(outfile == stderr); |
| 108 | } |