main.c 10.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
/*
 * gload.c: game loader for Ultra64 development system.
 *
 * Copyright 1995, Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
 * the contents of this file may not be disclosed to third parties, copied or
 * duplicated in any form, in whole or in part, without the prior written
 * permission of Silicon Graphics, Inc.
 *
 * RESTRICTED RIGHTS LEGEND:
 * Use, duplication or disclosure by the Government is subject to restrictions
 * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
 * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
 * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
 * rights reserved under the Copyright Laws of the United States.
 *
 * $Revision: 1.1.1.1 $
 */

#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <bstring.h>
#include <string.h>
#include <getopt.h>

#include <sys/u64gio.h>
#include <ramrom.h>

#include "gload.h"



static void usage(void);
static int readNverify(int u64fd, unsigned int ramromOffset, int bufSize, 
			unsigned char *buf, int verbose);
static int writeNverify(int u64fd, unsigned int ramromOffset, int bufSize, 
			unsigned char *buf, int verbose);

char *progName;

char *defaultRomName = "rom";

int Verify = 0;

main(int argc, char *argv[])
{
    int romFd;
    char errMessage[256];
    char appargs[256];
    char *fmtFileName, *romFileName;
    int u64fd, retval;
    struct stat buf;
    unsigned char *romBuf;
    int c, error;
    int romWordAlignedByteSize, appargWordAlignedByteSize;
    unsigned int romOffset;
    int romOffsetEntered = 0;
    int bootObjSize;
    int silent = 0;
    int verbose = 0;
    int errorsFound = 0;
    int noRom = 0;

    fmtFileName = NULL;
    progName = argv[0];

    strcpy(appargs, "\0");
    while ((c = getopt(argc, argv, "a:f:no:sVv")) != EOF) {
	switch (c) {
	    case 'a':
		strcpy(appargs, optarg);
		break;
	    case 'f':
		fmtFileName = optarg;
		break;
	    case 'n':
		noRom = 1;
		break;
	    case 'o':
		romOffset = strtol(optarg, NULL, 0);
		romOffsetEntered = 1;
		break;
	    case 's':
		silent = 1;
		break;
	    case 'V':
		Verify = 1;
		break;
	    case 'v':
		verbose = 1;
		break;
	    case '?':
		usage();
		exit(1);
       }
    }

    if(!noRom)
    {
	if (!romOffsetEntered) {
	    romOffset = 0;  /* rom will internally offset game to RAMROM_GAME_OFFSET */
	}

	/*
         * Last arg is our rom filename; provide a default if none was provided.
         */
	if ((argc - optind) != 1) {
	    romFileName = defaultRomName;
	} else {
	    romFileName = argv[optind];
	}

	if ((romFd = open(romFileName, O_RDONLY | O_NOCTTY)) < 0) {
	    sprintf(errMessage, "%s: unable to open %s", progName, romFileName);
	    perror(errMessage);
	    exit(1);
	}

	if ((u64fd = open(DEV_U64, O_RDWR | O_NOCTTY)) < 0) {
	    sprintf(errMessage, "%s unable to open %s", progName, DEV_U64);
	    perror(errMessage);
	    close(romFd);
	    exit(1);
	} 

	/*
         * Assert RESET
         */
	if ( (retval = ioctl(u64fd, U64_RESET, 1) ) < 0 ) {
	    sprintf(errMessage, "%s after U64_RESET ioctl", progName);
	    perror(errMessage);
	    close(romFd);
	    exit(1);
	} 
    }
    /*
     * If gload is going to act as a print/log server, then make sure that
     * it can listen to the events before we start loading the ramrom.
     */
    if (!silent) {
	if (printServerInit(fmtFileName) < 0) {
	    close(romFd);
    	    exit(1);
	}
    }


    if(!noRom)
    {
	/*
     * Load the ROM
     */
	if (fstat(romFd, &buf) < 0) {
	    sprintf(errMessage, "%s unable to stat %s", progName, romFileName);
	    perror(errMessage);
	    close(romFd);
	    exit(1);
	}
	romWordAlignedByteSize = buf.st_size;

	romBuf = malloc(romWordAlignedByteSize);
	if (romBuf == NULL) {
	    fprintf(stderr, "%s: unable to malloc buffer to hold %d bytes\n", 
		    progName, romWordAlignedByteSize);
	    close(romFd);
	    exit(1);
	}

	retval = read(romFd, (void *)romBuf, romWordAlignedByteSize);

	if (retval != romWordAlignedByteSize) {
	    fprintf(stderr, "%s: short read from %s.\n", progName, romFileName);
	    free(romBuf);
	    close(romFd);
	    exit(1);
	}

	/*
     * Write the contents of the ROM file into ramrom first.
     */
	if (writeNverify(u64fd, romOffset, romWordAlignedByteSize, romBuf, 
			 verbose) > 0) {
	    fprintf(stderr, "gload: write & readback of rom image failed.\n");
	    free(romBuf);
	    close(romFd);
	    close(u64fd);
	    exit(1);
	}
	close(romFd);

    /*
     * Write the application arguments into the app-to-indy comm buffer, if
     * any.
     */
	appargWordAlignedByteSize = strlen(appargs) +1;
    
	/* 
     * We must transfer an integral number of words.  Doesn't hurt to 
     * transfer a couple of extra bytes of crap.
     */
    
	if (appargWordAlignedByteSize & 3){
	    appargWordAlignedByteSize += 4;
	    appargWordAlignedByteSize &= ~(3);
	}
    
	if (writeNverify(u64fd, RAMROM_APP_WRITE_ADDR, 
			 appargWordAlignedByteSize, appargs, verbose) > 0) {
	    fprintf(stderr, "gload: write & readback of appargs failed.\n");
	    close(u64fd);
	    exit(1);
	}

	/*
     * Go back one more time & verify.  If the romoffset is zero, we will 
     * assume that this is the standard game rom that was loaded, and thus
     * we check RAMROM_GAME_OFFSET bytes into the rom (since we purposefully overwrote 
     * that portion of the rom).
     */
	if (romOffset == 0) {
	    romOffset = RAMROM_GAME_OFFSET;
	    if (retval = readNverify(u64fd, romOffset,
				     (romWordAlignedByteSize - romOffset), 
				     ( (unsigned char *)( ((int)romBuf) + romOffset ) ), 
				     verbose) != 0) {
		errorsFound = retval;
		fprintf(stderr, 
			"gload: final rom readback failed (%d errors).\n",
			retval);
	    }
	} else {
	    if (readNverify(u64fd, romOffset, romWordAlignedByteSize, 
			    romBuf, verbose) != 0) {
		errorsFound += retval;
		fprintf(stderr, 
			"gload: final rom comparison failed (%d errors).\n",
			retval);
	    }
	}

	/* if (readNverify(u64fd, RAMROM_BOOTSTRAP_OFFSET, bootWordAlignedByteSize, 
      bootBuf, verbose) != 0) {
	errorsFound += retval;
	fprintf(stderr, 
	  "gload: final bootstrap readback failed (%d errors).\n",
	  retval);
    }

    if (readNverify(u64fd, RAMROM_PIF2BOOTSTRAP_OFFSET, 
      pif2bootWordAlignedByteSize, pif2bootBuf, verbose) != 0) {
	errorsFound += retval;
	fprintf(stderr, 
	  "gload: final pif2 bootstrap readback failed (%d errors).\n",
	  retval);
    } */

	if (readNverify(u64fd, RAMROM_APP_WRITE_ADDR, appargWordAlignedByteSize,
			appargs, verbose) != 0) {
	    errorsFound += retval;
	    fprintf(stderr, 
		    "gload: final appargs readback failed (%d errors).\n",
		    retval);
	}

	/*
     * Do the romheader last; it leaves the dram page pointer parked at
     * page 0.  For now, this is just a placeholder!
     */
	if (readNverify(u64fd, 0x0, 4, romBuf, verbose) != 0) {
	    errorsFound += retval;
	    fprintf(stderr, 
		    "gload: final romheader readback failed (%d errors).\n",
		    retval);
	}


	/*
     * Release RESET; game will subsequently boot itself.
     */
	if ( (retval = ioctl(u64fd, U64_RESET, 0) ) < 0 ) {
	    sprintf(errMessage, "%s after U64_RESET ioctl", progName);
	    perror(errMessage);
	    close(u64fd);
	    exit(1);
	} 

	if (romBuf)
	    free(romBuf);
    }

    if (!silent) {
	if (printServer() < 0)
		errorsFound += 1;
    }

    close(u64fd);
    printServerClose();

    if (errorsFound)
	exit(1);
    else
	exit(0);
}

static void
usage(void)
{
    fprintf(stderr, 
"usage:\tgload\t[-a ""app args""] [-o rom offset within ramrom]\n");
    fprintf(stderr, 
"\t\t[-f log format file] [-V] [-v] [-s] [romfile]\n");
    exit(1);
}

/*
 * Returns number of errors encountered.
 */
int
writeNverify(int u64fd, unsigned int ramromOffset, int bufSize, 
	unsigned char *buf, int verbose)
{
    char errMessage[256];
    unsigned char *readBuf, *pSrc, *pDst;
    u64_write_arg_t arg;
    int retval, i, error;

    if ((ramromOffset + bufSize) > RAMROM_SIZE) {
	fprintf(stderr, 
	  "%s: (ramrom offset 0x%x) + (buffer size 0x%x) = 0x%x > 0x%x\n", 
	  ramromOffset, bufSize, (ramromOffset + bufSize), RAMROM_SIZE);
	return(1);
    }

    /*
     * Tell the kernel to copyin our data into ramrom.
     */
    arg.buffer = (void *)buf;
    arg.ramrom_addr = ramromOffset;
    arg.nbytes = bufSize;

    if ( (retval = ioctl(u64fd, U64_WRITE, &arg) ) < 0 ) {
	sprintf(errMessage, "%s after U64_WRITE ioctl, return value %d", 
	  progName, retval);
	perror(errMessage);
	return(1);
    } 

    if (!Verify) {
	return(0);
    }

    /*
     * Read back & verify, to ensure our load worked.
     */
    readBuf = malloc(bufSize);
    if (readBuf == NULL) {
	fprintf(stderr, "%s: unable to malloc buffer to hold %d bytes\n", 
	  progName, bufSize);
	return(1);
    }
    /*
     * Tell the kernel to copyout ramrom data into our buffer.
     */
    arg.buffer = (void *)readBuf;
    arg.ramrom_addr = ramromOffset;
    arg.nbytes = bufSize;

    if ( (retval = ioctl(u64fd, U64_READ, &arg) ) < 0 ) {
	sprintf(errMessage, "%s after U64_READ ioctl, return value %d", 
	  progName, retval);
	perror(errMessage);
	return(1);
    } 

    for (i = 0, error = 0, pSrc = buf, pDst = readBuf; 
      i < bufSize; i++) {
	if (*pSrc != *pDst) {
	    if (verbose) {
		fprintf(stderr, "Error (byte %d): read 0x%x, exp 0x%x\n", 
		  i, *pDst, *pSrc);
	    }
	    error++;
	}
	pSrc++;
	pDst++;
    }
    if (error > 0) {
	return(error);
    }

    return(0);
}

/*
 * Returns number of errors encountered.
 */
int
readNverify(int u64fd, unsigned int ramromOffset, int bufSize, 
	unsigned char *buf, int verbose)
{
    char errMessage[256];
    unsigned char *readBuf, *pSrc, *pDst;
    u64_write_arg_t arg;
    int retval, i, error;

    if (!Verify) {
	return(0);
    }

    if ((ramromOffset + bufSize) > RAMROM_SIZE) {
	fprintf(stderr, 
	  "%s: (ramrom offset 0x%x) + (buffer size 0x%x) = 0x%x > 0x%x\n", 
	  ramromOffset, bufSize, (ramromOffset + bufSize), RAMROM_SIZE);
	return(1);
    }

    /*
     * Read back & verify, to ensure our load worked.
     */
    readBuf = malloc(bufSize);
    if (readBuf == NULL) {
	fprintf(stderr, "%s: unable to malloc buffer to hold %d bytes\n", 
	  progName, bufSize);
	return(1);
    }
    /*
     * Tell the kernel to copyout ramrom data into our buffer.
     */
    arg.buffer = (void *)readBuf;
    arg.ramrom_addr = ramromOffset;
    arg.nbytes = bufSize;

    if ( (retval = ioctl(u64fd, U64_READ, &arg) ) < 0 ) {
	sprintf(errMessage, "%s after U64_READ ioctl, return value %d", 
	  progName, retval);
	perror(errMessage);
	return(1);
    } 

    for (i = 0, error = 0, pSrc = buf, pDst = readBuf; 
      i < bufSize; i++) {
	if (*pSrc != *pDst) {
	    if (verbose) {
		fprintf(stderr, "Error (byte %d): read 0x%x, exp 0x%x\n", 
		  i, *pDst, *pSrc);
	    }
	    error++;
	}
	pSrc++;
	pDst++;
    }
    if (error > 0) {
	return(error);
    }
    return(0);
}