/*
	Change the system id on an IP30/Octane running Irix 6.5 
	Only tested on 2 Octanes running Irix 6.5.19f
		-- take that for what it's worth.

	Placed in public domain by the author qball@ansic.net - 2003
	Based on PD Code by ratatosk@mail2me.com Placed in PD - 1996

	No warranty at all with this software; it may cause a nuclear reaction
	in your machine.

	Instructions:
		cc -o sidchange-octane-ip30 sidchange-octane-ip30.c -lelf
		./sidchange-octane-ip30 newsysid
	
	Notes: 
		You must run this program as root
		Run with no arguements this should print your current system id
		Do this first as a test.

	Cheap Advertisement: #C irc.dal.net
*/
	
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <nlist.h>
#include <string.h>
#include <errno.h>

int 
main(int argc, char *argv[])
{
	int kmem;
	unsigned int new_id, cur_id;
	off_t where;
   	struct nlist64 nl[2];
 
	nl[0].n_name="is_octane_lx";
	nl[1].n_name=NULL;

	if (argc == 2)
		new_id = strtoul(argv[1], NULL, 16);


	if ((kmem = open("/dev/kmem", O_RDWR)) < 0) {
		fprintf(stderr, "cannot open /dev/kmem\n");
		fprintf(stderr, "%s\n", strerror(errno));
		return 1;
	}
    
	if (nlist64("/unix", nl) < 0) {
		fprintf(stderr, "cannot read namelist out of /dev/ksyms\n");
		return 1;
	}
    
	if ((where = nl[0].n_value) == 0) {
		fprintf(stderr, "unknown kernel variable is_octane_lx\n");
		return 1;
	}
	
	if (lseek(kmem, where + 4, SEEK_SET) == (-1)) {
		fprintf(stderr, "lseek on /dev/kmem failed\n");
		return 1;
	}

	if (read(kmem, &cur_id, 4) < 4) {
		fprintf(stderr, "read from /dev/kmem failed\n");
		fprintf(stderr, "%s\n", strerror(errno));
		return 1;
	}
	printf("The System ID Is Currently: %08x\n", cur_id);

	if (argc == 2) {
		if (lseek(kmem, where + 4, SEEK_SET) == (-1)) {
			fprintf(stderr, "lseek on /dev/kmem failed\n");
			return 1;
		}
		if (write(kmem, &new_id, 4) < 4) {
			fprintf(stderr, "write to /dev/kmem failed\n");
			fprintf(stderr, "%s\n", strerror(errno));
			return 1;
		}
		printf("The System ID Has Changed To: %08x\n",new_id);
	}
	close(kmem);
	return 0;
}


