Blame ownership.c

Packit a55458
/*
Packit a55458
 * Compaq Ownership Tag
Packit a55458
 *
Packit a55458
 *   Copyright (C) 2003-2005 Jean Delvare <jdelvare@suse.de>
Packit a55458
 *
Packit a55458
 *   This program is free software; you can redistribute it and/or modify
Packit a55458
 *   it under the terms of the GNU General Public License as published by
Packit a55458
 *   the Free Software Foundation; either version 2 of the License, or
Packit a55458
 *   (at your option) any later version.
Packit a55458
 *
Packit a55458
 *   This program is distributed in the hope that it will be useful,
Packit a55458
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a55458
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit a55458
 *   GNU General Public License for more details.
Packit a55458
 *
Packit a55458
 *   You should have received a copy of the GNU General Public License
Packit a55458
 *   along with this program; if not, write to the Free Software
Packit a55458
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
Packit a55458
 *
Packit a55458
 *   For the avoidance of doubt the "preferred form" of this code is one which
Packit a55458
 *   is in an open unpatent encumbered format. Where cryptographic key signing
Packit a55458
 *   forms part of the process of creating an executable the information
Packit a55458
 *   including keys needed to generate an equivalently functional executable
Packit a55458
 *   are deemed to be part of the source code.
Packit a55458
 *
Packit a55458
 * References:
Packit a55458
 *  - Compaq "Technical Reference Guide for Compaq Deskpro 4000 and 6000"
Packit a55458
 *    First Edition
Packit a55458
 *    http://h18000.www1.hp.com/support/techpubs/technical_reference_guides/113a1097.html
Packit a55458
 */
Packit a55458
Packit a55458
#include <stdio.h>
Packit a55458
#include <stdlib.h>
Packit a55458
#include <string.h>
Packit a55458
#include <unistd.h>
Packit a55458
#include <getopt.h>
Packit a55458
Packit a55458
#include "version.h"
Packit a55458
#include "config.h"
Packit a55458
#include "types.h"
Packit a55458
#include "util.h"
Packit a55458
Packit a55458
/* Options are global */
Packit a55458
struct opt
Packit a55458
{
Packit a55458
	const char *devmem;
Packit a55458
	unsigned int flags;
Packit a55458
};
Packit a55458
static struct opt opt;
Packit a55458
Packit a55458
#define FLAG_VERSION            (1 << 0)
Packit a55458
#define FLAG_HELP               (1 << 1)
Packit a55458
Packit a55458
static void ownership(u32 base, const char *pname, const char *devmem)
Packit a55458
{
Packit a55458
	u8 *buf;
Packit a55458
	int i;
Packit a55458
Packit a55458
	/* read the ownership tag */
Packit a55458
	if ((buf = mem_chunk(base, 0x51, devmem)) == NULL)
Packit a55458
	{
Packit a55458
		perror(pname);
Packit a55458
		return;
Packit a55458
	}
Packit a55458
Packit a55458
	/* chop the trailing garbage */
Packit a55458
	i = 0x4f;
Packit a55458
	while (i >= 0 && (buf[i] == 0x20 || buf[i] == 0x00))
Packit a55458
		i--;
Packit a55458
	buf[i + 1] = '\0';
Packit a55458
Packit a55458
	/* filter and print */
Packit a55458
	if (i >= 0)
Packit a55458
	{
Packit a55458
		for (; i >= 0; i--)
Packit a55458
		{
Packit a55458
			if (buf[i] < 32 || (buf[i] >= 127 && buf[i] < 160))
Packit a55458
				buf[i] = '?';
Packit a55458
		}
Packit a55458
		printf("%s\n", (char *)buf);
Packit a55458
	}
Packit a55458
Packit a55458
	free(buf);
Packit a55458
}
Packit a55458
Packit a55458
static u32 decode(const u8 *p)
Packit a55458
{
Packit a55458
	int i;
Packit a55458
Packit a55458
	/* integrity checking (lack of checksum) */
Packit a55458
	for (i = 0; i < p[4]; i++)
Packit a55458
	{
Packit a55458
		if (p[5 + i * 10] != '$'
Packit a55458
		 || !(p[6 + i * 10] >= 'A' && p[6 + i * 10] <= 'Z')
Packit a55458
		 || !(p[7 + i * 10] >= 'A' && p[7 + i * 10] <= 'Z')
Packit a55458
		 || !(p[8 + i * 10] >= 'A' && p[8 + i * 10] <= 'Z'))
Packit a55458
		{
Packit a55458
			printf("\t Abnormal Entry! Please report. [%02x %02x %02x %02x]\n",
Packit a55458
				p[5 + i * 10], p[6 + i * 10],
Packit a55458
				p[7 + i * 10], p[8 + i * 10]);
Packit a55458
			return 0;
Packit a55458
		}
Packit a55458
	}
Packit a55458
Packit a55458
	/* search for the right entry */
Packit a55458
	for (i = 0; i < p[4]; i++)
Packit a55458
		if (memcmp(p + 5 + i * 10, "$ERB", 4) == 0)
Packit a55458
			return DWORD(p + 9 + i * 10);
Packit a55458
Packit a55458
	return 0;
Packit a55458
}
Packit a55458
Packit a55458
/* Return -1 on error, 0 on success */
Packit a55458
static int parse_command_line(int argc, char * const argv[])
Packit a55458
{
Packit a55458
	int option;
Packit a55458
	const char *optstring = "d:hV";
Packit a55458
	struct option longopts[] = {
Packit a55458
		{ "dev-mem", required_argument, NULL, 'd' },
Packit a55458
		{ "help", no_argument, NULL, 'h' },
Packit a55458
		{ "version", no_argument, NULL, 'V' },
Packit a55458
		{ NULL, 0, NULL, 0 }
Packit a55458
	};
Packit a55458
Packit a55458
	while ((option = getopt_long(argc, argv, optstring, longopts, NULL)) != -1)
Packit a55458
		switch (option)
Packit a55458
		{
Packit a55458
			case 'd':
Packit a55458
				opt.devmem = optarg;
Packit a55458
				break;
Packit a55458
			case 'h':
Packit a55458
				opt.flags |= FLAG_HELP;
Packit a55458
				break;
Packit a55458
			case 'V':
Packit a55458
				opt.flags |= FLAG_VERSION;
Packit a55458
				break;
Packit a55458
			case '?':
Packit a55458
				return -1;
Packit a55458
		}
Packit a55458
Packit a55458
	return 0;
Packit a55458
}
Packit a55458
Packit a55458
static void print_help(void)
Packit a55458
{
Packit a55458
	static const char *help =
Packit a55458
		"Usage: ownership [OPTIONS]\n"
Packit a55458
		"Options are:\n"
Packit a55458
		" -d, --dev-mem FILE     Read memory from device FILE (default: " DEFAULT_MEM_DEV ")\n"
Packit a55458
		" -h, --help             Display this help text and exit\n"
Packit a55458
		" -V, --version          Display the version and exit\n";
Packit a55458
Packit a55458
	printf("%s", help);
Packit a55458
}
Packit a55458
Packit a55458
int main(int argc, char * const argv[])
Packit a55458
{
Packit a55458
	u8 *buf;
Packit a55458
	off_t fp;
Packit a55458
	int ok = 0;
Packit a55458
Packit a55458
	if (sizeof(u8) != 1 || sizeof(u32) != 4)
Packit a55458
	{
Packit a55458
		fprintf(stderr, "%s: compiler incompatibility\n", argv[0]);
Packit a55458
		exit(255);
Packit a55458
	}
Packit a55458
Packit a55458
	/* Set default option values */
Packit a55458
	opt.devmem = DEFAULT_MEM_DEV;
Packit a55458
	opt.flags = 0;
Packit a55458
Packit a55458
	if (parse_command_line(argc, argv)<0)
Packit a55458
		exit(2);
Packit a55458
Packit a55458
	if (opt.flags & FLAG_HELP)
Packit a55458
	{
Packit a55458
		print_help();
Packit a55458
		return 0;
Packit a55458
	}
Packit a55458
Packit a55458
	if (opt.flags & FLAG_VERSION)
Packit a55458
	{
Packit a55458
		printf("%s\n", VERSION);
Packit a55458
		return 0;
Packit a55458
	}
Packit a55458
Packit a55458
	if ((buf = mem_chunk(0xE0000, 0x20000, opt.devmem)) == NULL)
Packit a55458
		exit(1);
Packit a55458
Packit a55458
	for (fp = 0; !ok && fp <= 0x1FFF0; fp += 16)
Packit a55458
	{
Packit a55458
		u8 *p = buf + fp;
Packit a55458
Packit a55458
		if (memcmp((char *)p, "32OS", 4) == 0)
Packit a55458
		{
Packit a55458
			off_t len = p[4] * 10 + 5;
Packit a55458
Packit a55458
			if (fp + len - 1 <= 0x1FFFF)
Packit a55458
			{
Packit a55458
				u32 base;
Packit a55458
Packit a55458
				if ((base = decode(p)))
Packit a55458
				{
Packit a55458
					ok = 1;
Packit a55458
					ownership(base, argv[0], opt.devmem);
Packit a55458
				}
Packit a55458
			}
Packit a55458
		}
Packit a55458
	}
Packit a55458
Packit a55458
	free(buf);
Packit a55458
Packit a55458
	return 0;
Packit a55458
}