"Fossies" - the Fresh Open Source Software Archive

Member "dmidecode-3.3/ownership.c" (14 Oct 2020, 4779 Bytes) of package /linux/privat/dmidecode-3.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ownership.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.12_vs_3.0.

    1 /*
    2  * Compaq Ownership Tag
    3  *
    4  *   Copyright (C) 2003-2005 Jean Delvare <jdelvare@suse.de>
    5  *
    6  *   This program is free software; you can redistribute it and/or modify
    7  *   it under the terms of the GNU General Public License as published by
    8  *   the Free Software Foundation; either version 2 of the License, or
    9  *   (at your option) any later version.
   10  *
   11  *   This program is distributed in the hope that it will be useful,
   12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  *   GNU General Public License for more details.
   15  *
   16  *   You should have received a copy of the GNU General Public License
   17  *   along with this program; if not, write to the Free Software
   18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   19  *
   20  *   For the avoidance of doubt the "preferred form" of this code is one which
   21  *   is in an open unpatent encumbered format. Where cryptographic key signing
   22  *   forms part of the process of creating an executable the information
   23  *   including keys needed to generate an equivalently functional executable
   24  *   are deemed to be part of the source code.
   25  *
   26  * References:
   27  *  - Compaq "Technical Reference Guide for Compaq Deskpro 4000 and 6000"
   28  *    First Edition
   29  *    http://h18000.www1.hp.com/support/techpubs/technical_reference_guides/113a1097.html
   30  */
   31 
   32 #include <stdio.h>
   33 #include <stdlib.h>
   34 #include <string.h>
   35 #include <unistd.h>
   36 #include <getopt.h>
   37 
   38 #include "version.h"
   39 #include "config.h"
   40 #include "types.h"
   41 #include "util.h"
   42 
   43 /* Options are global */
   44 struct opt
   45 {
   46     const char *devmem;
   47     unsigned int flags;
   48 };
   49 static struct opt opt;
   50 
   51 #define FLAG_VERSION            (1 << 0)
   52 #define FLAG_HELP               (1 << 1)
   53 
   54 static void ownership(u32 base, const char *pname, const char *devmem)
   55 {
   56     u8 *buf;
   57     int i;
   58 
   59     /* read the ownership tag */
   60     if ((buf = mem_chunk(base, 0x51, devmem)) == NULL)
   61     {
   62         perror(pname);
   63         return;
   64     }
   65 
   66     /* chop the trailing garbage */
   67     i = 0x4f;
   68     while (i >= 0 && (buf[i] == 0x20 || buf[i] == 0x00))
   69         i--;
   70     buf[i + 1] = '\0';
   71 
   72     /* filter and print */
   73     if (i >= 0)
   74     {
   75         for (; i >= 0; i--)
   76         {
   77             if (buf[i] < 32 || (buf[i] >= 127 && buf[i] < 160))
   78                 buf[i] = '?';
   79         }
   80         printf("%s\n", (char *)buf);
   81     }
   82 
   83     free(buf);
   84 }
   85 
   86 static u32 decode(const u8 *p)
   87 {
   88     int i;
   89 
   90     /* integrity checking (lack of checksum) */
   91     for (i = 0; i < p[4]; i++)
   92     {
   93         if (p[5 + i * 10] != '$'
   94          || !(p[6 + i * 10] >= 'A' && p[6 + i * 10] <= 'Z')
   95          || !(p[7 + i * 10] >= 'A' && p[7 + i * 10] <= 'Z')
   96          || !(p[8 + i * 10] >= 'A' && p[8 + i * 10] <= 'Z'))
   97         {
   98             printf("\t Abnormal Entry! Please report. [%02x %02x %02x %02x]\n",
   99                 p[5 + i * 10], p[6 + i * 10],
  100                 p[7 + i * 10], p[8 + i * 10]);
  101             return 0;
  102         }
  103     }
  104 
  105     /* search for the right entry */
  106     for (i = 0; i < p[4]; i++)
  107         if (memcmp(p + 5 + i * 10, "$ERB", 4) == 0)
  108             return DWORD(p + 9 + i * 10);
  109 
  110     return 0;
  111 }
  112 
  113 /* Return -1 on error, 0 on success */
  114 static int parse_command_line(int argc, char * const argv[])
  115 {
  116     int option;
  117     const char *optstring = "d:hV";
  118     struct option longopts[] = {
  119         { "dev-mem", required_argument, NULL, 'd' },
  120         { "help", no_argument, NULL, 'h' },
  121         { "version", no_argument, NULL, 'V' },
  122         { NULL, 0, NULL, 0 }
  123     };
  124 
  125     while ((option = getopt_long(argc, argv, optstring, longopts, NULL)) != -1)
  126         switch (option)
  127         {
  128             case 'd':
  129                 opt.devmem = optarg;
  130                 break;
  131             case 'h':
  132                 opt.flags |= FLAG_HELP;
  133                 break;
  134             case 'V':
  135                 opt.flags |= FLAG_VERSION;
  136                 break;
  137             case '?':
  138                 return -1;
  139         }
  140 
  141     return 0;
  142 }
  143 
  144 static void print_help(void)
  145 {
  146     static const char *help =
  147         "Usage: ownership [OPTIONS]\n"
  148         "Options are:\n"
  149         " -d, --dev-mem FILE     Read memory from device FILE (default: " DEFAULT_MEM_DEV ")\n"
  150         " -h, --help             Display this help text and exit\n"
  151         " -V, --version          Display the version and exit\n";
  152 
  153     printf("%s", help);
  154 }
  155 
  156 int main(int argc, char * const argv[])
  157 {
  158     u8 *buf;
  159     off_t fp;
  160     int ok = 0;
  161 
  162     if (sizeof(u8) != 1 || sizeof(u32) != 4)
  163     {
  164         fprintf(stderr, "%s: compiler incompatibility\n", argv[0]);
  165         exit(255);
  166     }
  167 
  168     /* Set default option values */
  169     opt.devmem = DEFAULT_MEM_DEV;
  170     opt.flags = 0;
  171 
  172     if (parse_command_line(argc, argv)<0)
  173         exit(2);
  174 
  175     if (opt.flags & FLAG_HELP)
  176     {
  177         print_help();
  178         return 0;
  179     }
  180 
  181     if (opt.flags & FLAG_VERSION)
  182     {
  183         printf("%s\n", VERSION);
  184         return 0;
  185     }
  186 
  187     if ((buf = mem_chunk(0xE0000, 0x20000, opt.devmem)) == NULL)
  188         exit(1);
  189 
  190     for (fp = 0; !ok && fp <= 0x1FFF0; fp += 16)
  191     {
  192         u8 *p = buf + fp;
  193 
  194         if (memcmp((char *)p, "32OS", 4) == 0)
  195         {
  196             off_t len = p[4] * 10 + 5;
  197 
  198             if (fp + len - 1 <= 0x1FFFF)
  199             {
  200                 u32 base;
  201 
  202                 if ((base = decode(p)))
  203                 {
  204                     ok = 1;
  205                     ownership(base, argv[0], opt.devmem);
  206                 }
  207             }
  208         }
  209     }
  210 
  211     free(buf);
  212 
  213     return 0;
  214 }