|  | @@ -0,0 +1,284 @@
 | 
		
	
		
			
			|  | 1 | +/*
 | 
		
	
		
			
			|  | 2 | + * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
 | 
		
	
		
			
			|  | 3 | + *
 | 
		
	
		
			
			|  | 4 | + * This program is free software; you can redistribute it and/or
 | 
		
	
		
			
			|  | 5 | + * modify it under the terms of the GNU General Public License as
 | 
		
	
		
			
			|  | 6 | + * published by the Free Software Foundation; either version 2 of the
 | 
		
	
		
			
			|  | 7 | + * License, or (at your option) any later version.
 | 
		
	
		
			
			|  | 8 | + *
 | 
		
	
		
			
			|  | 9 | + * This program is distributed in the hope that it will be useful, but
 | 
		
	
		
			
			|  | 10 | + * WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
		
	
		
			
			|  | 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
		
	
		
			
			|  | 12 | + * General Public License for more details.
 | 
		
	
		
			
			|  | 13 | + *
 | 
		
	
		
			
			|  | 14 | + * You should have received a copy of the GNU General Public License
 | 
		
	
		
			
			|  | 15 | + * along with this program; if not, write to the Free Software
 | 
		
	
		
			
			|  | 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 | 
		
	
		
			
			|  | 17 | + * 02110-1301, USA.
 | 
		
	
		
			
			|  | 18 | + *
 | 
		
	
		
			
			|  | 19 | + * You can also choose to distribute this program under the terms of
 | 
		
	
		
			
			|  | 20 | + * the Unmodified Binary Distribution Licence (as given in the file
 | 
		
	
		
			
			|  | 21 | + * COPYING.UBDL), provided that you have satisfied its requirements.
 | 
		
	
		
			
			|  | 22 | + */
 | 
		
	
		
			
			|  | 23 | +
 | 
		
	
		
			
			|  | 24 | +FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 | 
		
	
		
			
			|  | 25 | +
 | 
		
	
		
			
			|  | 26 | +#include <stdint.h>
 | 
		
	
		
			
			|  | 27 | +#include <string.h>
 | 
		
	
		
			
			|  | 28 | +#include <errno.h>
 | 
		
	
		
			
			|  | 29 | +#include <ipxe/console.h>
 | 
		
	
		
			
			|  | 30 | +#include <ipxe/init.h>
 | 
		
	
		
			
			|  | 31 | +#include <realmode.h>
 | 
		
	
		
			
			|  | 32 | +#include <int13.h>
 | 
		
	
		
			
			|  | 33 | +#include <config/console.h>
 | 
		
	
		
			
			|  | 34 | +
 | 
		
	
		
			
			|  | 35 | +/** @file
 | 
		
	
		
			
			|  | 36 | + *
 | 
		
	
		
			
			|  | 37 | + * INT13 disk log console
 | 
		
	
		
			
			|  | 38 | + *
 | 
		
	
		
			
			|  | 39 | + */
 | 
		
	
		
			
			|  | 40 | +
 | 
		
	
		
			
			|  | 41 | +/* Set default console usage if applicable */
 | 
		
	
		
			
			|  | 42 | +#if ! ( defined ( CONSOLE_INT13 ) && CONSOLE_EXPLICIT ( CONSOLE_INT13 ) )
 | 
		
	
		
			
			|  | 43 | +#undef CONSOLE_INT13
 | 
		
	
		
			
			|  | 44 | +#define CONSOLE_INT13 ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
 | 
		
	
		
			
			|  | 45 | +#endif
 | 
		
	
		
			
			|  | 46 | +
 | 
		
	
		
			
			|  | 47 | +/** Disk drive number */
 | 
		
	
		
			
			|  | 48 | +#define INT13CON_DRIVE 0x80
 | 
		
	
		
			
			|  | 49 | +
 | 
		
	
		
			
			|  | 50 | +/** Log partition type */
 | 
		
	
		
			
			|  | 51 | +#define INT13CON_PARTITION_TYPE 0xe0
 | 
		
	
		
			
			|  | 52 | +
 | 
		
	
		
			
			|  | 53 | +/** Maximum number of outstanding unwritten characters */
 | 
		
	
		
			
			|  | 54 | +#define INT13CON_MAX_UNWRITTEN 64
 | 
		
	
		
			
			|  | 55 | +
 | 
		
	
		
			
			|  | 56 | +/** Log partition header */
 | 
		
	
		
			
			|  | 57 | +struct int13con_header {
 | 
		
	
		
			
			|  | 58 | +	/** Magic signature */
 | 
		
	
		
			
			|  | 59 | +	char magic[10];
 | 
		
	
		
			
			|  | 60 | +} __attribute__ (( packed ));
 | 
		
	
		
			
			|  | 61 | +
 | 
		
	
		
			
			|  | 62 | +/** Log partition magic signature */
 | 
		
	
		
			
			|  | 63 | +#define INT13CON_MAGIC "iPXE LOG\n\n"
 | 
		
	
		
			
			|  | 64 | +
 | 
		
	
		
			
			|  | 65 | +/** Sector buffer */
 | 
		
	
		
			
			|  | 66 | +static uint8_t __bss16_array ( int13con_buffer, [INT13_BLKSIZE] );
 | 
		
	
		
			
			|  | 67 | +#define int13con_buffer __use_data16 ( int13con_buffer )
 | 
		
	
		
			
			|  | 68 | +
 | 
		
	
		
			
			|  | 69 | +/** Disk address packet */
 | 
		
	
		
			
			|  | 70 | +static struct int13_disk_address __bss16 ( int13con_address );
 | 
		
	
		
			
			|  | 71 | +#define int13con_address __use_data16 ( int13con_address )
 | 
		
	
		
			
			|  | 72 | +
 | 
		
	
		
			
			|  | 73 | +/** Current LBA */
 | 
		
	
		
			
			|  | 74 | +static uint64_t int13con_lba;
 | 
		
	
		
			
			|  | 75 | +
 | 
		
	
		
			
			|  | 76 | +/** Maximum LBA */
 | 
		
	
		
			
			|  | 77 | +static uint64_t int13con_max_lba;
 | 
		
	
		
			
			|  | 78 | +
 | 
		
	
		
			
			|  | 79 | +/** Current offset within sector */
 | 
		
	
		
			
			|  | 80 | +static size_t int13con_offset;
 | 
		
	
		
			
			|  | 81 | +
 | 
		
	
		
			
			|  | 82 | +/** Number of unwritten characters */
 | 
		
	
		
			
			|  | 83 | +static size_t int13con_unwritten;
 | 
		
	
		
			
			|  | 84 | +
 | 
		
	
		
			
			|  | 85 | +struct console_driver int13con __console_driver;
 | 
		
	
		
			
			|  | 86 | +
 | 
		
	
		
			
			|  | 87 | +/**
 | 
		
	
		
			
			|  | 88 | + * Read/write disk sector
 | 
		
	
		
			
			|  | 89 | + *
 | 
		
	
		
			
			|  | 90 | + * @v op		Operation
 | 
		
	
		
			
			|  | 91 | + * @v lba		Logical block address
 | 
		
	
		
			
			|  | 92 | + * @ret rc		Return status code
 | 
		
	
		
			
			|  | 93 | + */
 | 
		
	
		
			
			|  | 94 | +static int int13con_rw ( unsigned int op, uint64_t lba ) {
 | 
		
	
		
			
			|  | 95 | +	uint8_t error;
 | 
		
	
		
			
			|  | 96 | +
 | 
		
	
		
			
			|  | 97 | +	/* Construct disk address packet */
 | 
		
	
		
			
			|  | 98 | +	int13con_address.bufsize = sizeof ( int13con_address );
 | 
		
	
		
			
			|  | 99 | +	int13con_address.count = 1;
 | 
		
	
		
			
			|  | 100 | +	int13con_address.buffer.segment = rm_ds;
 | 
		
	
		
			
			|  | 101 | +	int13con_address.buffer.offset = __from_data16 ( int13con_buffer );
 | 
		
	
		
			
			|  | 102 | +	int13con_address.lba = lba;
 | 
		
	
		
			
			|  | 103 | +
 | 
		
	
		
			
			|  | 104 | +	/* Issue INT13 */
 | 
		
	
		
			
			|  | 105 | +	__asm__ ( REAL_CODE ( "int $0x13\n\t" )
 | 
		
	
		
			
			|  | 106 | +		  : "=a" ( error )
 | 
		
	
		
			
			|  | 107 | +		  : "0" ( op << 8 ), "d" ( INT13CON_DRIVE ),
 | 
		
	
		
			
			|  | 108 | +		    "S" ( __from_data16 ( &int13con_address ) ) );
 | 
		
	
		
			
			|  | 109 | +	if ( error ) {
 | 
		
	
		
			
			|  | 110 | +		DBG ( "INT13CON operation %04x failed: %02x\n",
 | 
		
	
		
			
			|  | 111 | +		      op, error );
 | 
		
	
		
			
			|  | 112 | +		return -EIO;
 | 
		
	
		
			
			|  | 113 | +	}
 | 
		
	
		
			
			|  | 114 | +
 | 
		
	
		
			
			|  | 115 | +	return 0;
 | 
		
	
		
			
			|  | 116 | +}
 | 
		
	
		
			
			|  | 117 | +
 | 
		
	
		
			
			|  | 118 | +/**
 | 
		
	
		
			
			|  | 119 | + * Write character to console
 | 
		
	
		
			
			|  | 120 | + *
 | 
		
	
		
			
			|  | 121 | + * @v character		Character
 | 
		
	
		
			
			|  | 122 | + */
 | 
		
	
		
			
			|  | 123 | +static void int13con_putchar ( int character ) {
 | 
		
	
		
			
			|  | 124 | +	static int busy;
 | 
		
	
		
			
			|  | 125 | +	int rc;
 | 
		
	
		
			
			|  | 126 | +
 | 
		
	
		
			
			|  | 127 | +	/* Ignore if we are already mid-logging */
 | 
		
	
		
			
			|  | 128 | +	if ( busy )
 | 
		
	
		
			
			|  | 129 | +		return;
 | 
		
	
		
			
			|  | 130 | +	busy = 1;
 | 
		
	
		
			
			|  | 131 | +
 | 
		
	
		
			
			|  | 132 | +	/* Write character to buffer */
 | 
		
	
		
			
			|  | 133 | +	int13con_buffer[int13con_offset++] = character;
 | 
		
	
		
			
			|  | 134 | +	int13con_unwritten++;
 | 
		
	
		
			
			|  | 135 | +
 | 
		
	
		
			
			|  | 136 | +	/* Write sector to disk, if applicable */
 | 
		
	
		
			
			|  | 137 | +	if ( ( int13con_offset == INT13_BLKSIZE ) ||
 | 
		
	
		
			
			|  | 138 | +	     ( int13con_unwritten == INT13CON_MAX_UNWRITTEN ) ||
 | 
		
	
		
			
			|  | 139 | +	     ( character == '\n' ) ) {
 | 
		
	
		
			
			|  | 140 | +
 | 
		
	
		
			
			|  | 141 | +		/* Write sector to disk */
 | 
		
	
		
			
			|  | 142 | +		if ( ( rc = int13con_rw ( INT13_EXTENDED_WRITE,
 | 
		
	
		
			
			|  | 143 | +					  int13con_lba ) ) != 0 ) {
 | 
		
	
		
			
			|  | 144 | +			DBG ( "INT13CON could not write log\n" );
 | 
		
	
		
			
			|  | 145 | +			/* Ignore and continue; there's nothing we can do */
 | 
		
	
		
			
			|  | 146 | +		}
 | 
		
	
		
			
			|  | 147 | +
 | 
		
	
		
			
			|  | 148 | +		/* Reset count of unwritten characters */
 | 
		
	
		
			
			|  | 149 | +		int13con_unwritten = 0;
 | 
		
	
		
			
			|  | 150 | +	}
 | 
		
	
		
			
			|  | 151 | +
 | 
		
	
		
			
			|  | 152 | +	/* Move to next sector, if applicable */
 | 
		
	
		
			
			|  | 153 | +	if ( int13con_offset == INT13_BLKSIZE ) {
 | 
		
	
		
			
			|  | 154 | +
 | 
		
	
		
			
			|  | 155 | +		/* Disable console if we have run out of space */
 | 
		
	
		
			
			|  | 156 | +		if ( int13con_lba >= int13con_max_lba )
 | 
		
	
		
			
			|  | 157 | +			int13con.disabled = 1;
 | 
		
	
		
			
			|  | 158 | +
 | 
		
	
		
			
			|  | 159 | +		/* Clear log buffer */
 | 
		
	
		
			
			|  | 160 | +		memset ( int13con_buffer, 0, sizeof ( int13con_buffer ) );
 | 
		
	
		
			
			|  | 161 | +		int13con_offset = 0;
 | 
		
	
		
			
			|  | 162 | +
 | 
		
	
		
			
			|  | 163 | +		/* Move to next sector */
 | 
		
	
		
			
			|  | 164 | +		int13con_lba++;
 | 
		
	
		
			
			|  | 165 | +	}
 | 
		
	
		
			
			|  | 166 | +
 | 
		
	
		
			
			|  | 167 | +	/* Clear busy flag */
 | 
		
	
		
			
			|  | 168 | +	busy = 0;
 | 
		
	
		
			
			|  | 169 | +}
 | 
		
	
		
			
			|  | 170 | +
 | 
		
	
		
			
			|  | 171 | +/**
 | 
		
	
		
			
			|  | 172 | + * Find log partition
 | 
		
	
		
			
			|  | 173 | + *
 | 
		
	
		
			
			|  | 174 | + * @ret rc		Return status code
 | 
		
	
		
			
			|  | 175 | + */
 | 
		
	
		
			
			|  | 176 | +static int int13con_find ( void ) {
 | 
		
	
		
			
			|  | 177 | +	struct master_boot_record *mbr =
 | 
		
	
		
			
			|  | 178 | +		( ( struct master_boot_record * ) int13con_buffer );
 | 
		
	
		
			
			|  | 179 | +	struct int13con_header *hdr =
 | 
		
	
		
			
			|  | 180 | +		( ( struct int13con_header * ) int13con_buffer );
 | 
		
	
		
			
			|  | 181 | +	struct partition_table_entry part[4];
 | 
		
	
		
			
			|  | 182 | +	unsigned int i;
 | 
		
	
		
			
			|  | 183 | +	int rc;
 | 
		
	
		
			
			|  | 184 | +
 | 
		
	
		
			
			|  | 185 | +	/* Read MBR */
 | 
		
	
		
			
			|  | 186 | +	if ( ( rc = int13con_rw ( INT13_EXTENDED_READ, 0 ) ) != 0 ) {
 | 
		
	
		
			
			|  | 187 | +		DBG ( "INT13CON could not read MBR: %s\n", strerror ( rc ) );
 | 
		
	
		
			
			|  | 188 | +		return rc;
 | 
		
	
		
			
			|  | 189 | +	}
 | 
		
	
		
			
			|  | 190 | +
 | 
		
	
		
			
			|  | 191 | +	/* Check MBR magic */
 | 
		
	
		
			
			|  | 192 | +	if ( mbr->magic != INT13_MBR_MAGIC ) {
 | 
		
	
		
			
			|  | 193 | +		DBG ( "INT13CON incorrect MBR magic\n" );
 | 
		
	
		
			
			|  | 194 | +		DBG2_HDA ( 0, mbr, sizeof ( *mbr ) );
 | 
		
	
		
			
			|  | 195 | +		return -EINVAL;
 | 
		
	
		
			
			|  | 196 | +	}
 | 
		
	
		
			
			|  | 197 | +
 | 
		
	
		
			
			|  | 198 | +	/* Look for magic partition */
 | 
		
	
		
			
			|  | 199 | +	memcpy ( part, mbr->partitions, sizeof ( part ) );
 | 
		
	
		
			
			|  | 200 | +	for ( i = 0 ; i < ( sizeof ( part ) / sizeof ( part[0] ) ) ; i++ ) {
 | 
		
	
		
			
			|  | 201 | +
 | 
		
	
		
			
			|  | 202 | +		/* Skip partitions of the wrong type */
 | 
		
	
		
			
			|  | 203 | +		if ( part[i].type != INT13CON_PARTITION_TYPE )
 | 
		
	
		
			
			|  | 204 | +			continue;
 | 
		
	
		
			
			|  | 205 | +
 | 
		
	
		
			
			|  | 206 | +		/* Read partition header */
 | 
		
	
		
			
			|  | 207 | +		if ( ( rc = int13con_rw ( INT13_EXTENDED_READ,
 | 
		
	
		
			
			|  | 208 | +					  part[i].start ) ) != 0 ) {
 | 
		
	
		
			
			|  | 209 | +			DBG ( "INT13CON partition %d could not read header: "
 | 
		
	
		
			
			|  | 210 | +			      "%s\n", ( i + 1 ), strerror ( rc ) );
 | 
		
	
		
			
			|  | 211 | +			continue;
 | 
		
	
		
			
			|  | 212 | +		}
 | 
		
	
		
			
			|  | 213 | +
 | 
		
	
		
			
			|  | 214 | +		/* Check partition header */
 | 
		
	
		
			
			|  | 215 | +		if ( memcmp ( hdr->magic, INT13CON_MAGIC,
 | 
		
	
		
			
			|  | 216 | +			      sizeof ( hdr->magic ) ) != 0 ) {
 | 
		
	
		
			
			|  | 217 | +			DBG ( "INT13CON partition %d bad magic\n", ( i + 1 ) );
 | 
		
	
		
			
			|  | 218 | +			DBG2_HDA ( 0, hdr, sizeof ( *hdr ) );
 | 
		
	
		
			
			|  | 219 | +			continue;
 | 
		
	
		
			
			|  | 220 | +		}
 | 
		
	
		
			
			|  | 221 | +
 | 
		
	
		
			
			|  | 222 | +		/* Found log partition */
 | 
		
	
		
			
			|  | 223 | +		DBG ( "INT13CON partition %d at [%08x,%08x)\n", ( i + 1 ),
 | 
		
	
		
			
			|  | 224 | +		      part[i].start, ( part[i].start + part[i].length ) );
 | 
		
	
		
			
			|  | 225 | +		int13con_lba = part[i].start;
 | 
		
	
		
			
			|  | 226 | +		int13con_max_lba = ( part[i].start + part[i].length - 1 );
 | 
		
	
		
			
			|  | 227 | +
 | 
		
	
		
			
			|  | 228 | +		/* Initialise log buffer */
 | 
		
	
		
			
			|  | 229 | +		memset ( &int13con_buffer[ sizeof ( *hdr ) ], 0,
 | 
		
	
		
			
			|  | 230 | +			 ( sizeof ( int13con_buffer ) - sizeof ( *hdr ) ) );
 | 
		
	
		
			
			|  | 231 | +		int13con_offset = sizeof ( hdr->magic );
 | 
		
	
		
			
			|  | 232 | +
 | 
		
	
		
			
			|  | 233 | +		return 0;
 | 
		
	
		
			
			|  | 234 | +	}
 | 
		
	
		
			
			|  | 235 | +
 | 
		
	
		
			
			|  | 236 | +	DBG ( "INT13CON found no log partition\n" );
 | 
		
	
		
			
			|  | 237 | +	return -ENOENT;
 | 
		
	
		
			
			|  | 238 | +}
 | 
		
	
		
			
			|  | 239 | +
 | 
		
	
		
			
			|  | 240 | +/**
 | 
		
	
		
			
			|  | 241 | + * Initialise INT13 console
 | 
		
	
		
			
			|  | 242 | + *
 | 
		
	
		
			
			|  | 243 | + */
 | 
		
	
		
			
			|  | 244 | +static void int13con_init ( void ) {
 | 
		
	
		
			
			|  | 245 | +	uint8_t error;
 | 
		
	
		
			
			|  | 246 | +	uint16_t check;
 | 
		
	
		
			
			|  | 247 | +	unsigned int discard_c;
 | 
		
	
		
			
			|  | 248 | +	unsigned int discard_d;
 | 
		
	
		
			
			|  | 249 | +	int rc;
 | 
		
	
		
			
			|  | 250 | +
 | 
		
	
		
			
			|  | 251 | +	/* Check for INT13 extensions */
 | 
		
	
		
			
			|  | 252 | +	__asm__ __volatile__ ( REAL_CODE ( "int $0x13\n\t"
 | 
		
	
		
			
			|  | 253 | +					   "setc %%al\n\t" )
 | 
		
	
		
			
			|  | 254 | +			       : "=a" ( error ), "=b" ( check ),
 | 
		
	
		
			
			|  | 255 | +				 "=c" ( discard_c ), "=d" ( discard_d )
 | 
		
	
		
			
			|  | 256 | +			       : "0" ( INT13_EXTENSION_CHECK << 8 ),
 | 
		
	
		
			
			|  | 257 | +				 "1" ( 0x55aa ), "3" ( INT13CON_DRIVE ) );
 | 
		
	
		
			
			|  | 258 | +	if ( error || ( check != 0xaa55 ) ) {
 | 
		
	
		
			
			|  | 259 | +		DBG ( "INT13CON missing extensions (%02x,%04x)\n",
 | 
		
	
		
			
			|  | 260 | +		      error, check );
 | 
		
	
		
			
			|  | 261 | +		return;
 | 
		
	
		
			
			|  | 262 | +	}
 | 
		
	
		
			
			|  | 263 | +
 | 
		
	
		
			
			|  | 264 | +	/* Locate log partition */
 | 
		
	
		
			
			|  | 265 | +	if ( ( rc = int13con_find() ) != 0)
 | 
		
	
		
			
			|  | 266 | +		return;
 | 
		
	
		
			
			|  | 267 | +
 | 
		
	
		
			
			|  | 268 | +	/* Enable console */
 | 
		
	
		
			
			|  | 269 | +	int13con.disabled = 0;
 | 
		
	
		
			
			|  | 270 | +}
 | 
		
	
		
			
			|  | 271 | +
 | 
		
	
		
			
			|  | 272 | +/**
 | 
		
	
		
			
			|  | 273 | + * INT13 console initialisation function
 | 
		
	
		
			
			|  | 274 | + */
 | 
		
	
		
			
			|  | 275 | +struct init_fn int13con_init_fn __init_fn ( INIT_CONSOLE ) = {
 | 
		
	
		
			
			|  | 276 | +	.initialise = int13con_init,
 | 
		
	
		
			
			|  | 277 | +};
 | 
		
	
		
			
			|  | 278 | +
 | 
		
	
		
			
			|  | 279 | +/** INT13 console driver */
 | 
		
	
		
			
			|  | 280 | +struct console_driver int13con __console_driver = {
 | 
		
	
		
			
			|  | 281 | +	.putchar = int13con_putchar,
 | 
		
	
		
			
			|  | 282 | +	.disabled = CONSOLE_DISABLED,
 | 
		
	
		
			
			|  | 283 | +	.usage = CONSOLE_INT13,
 | 
		
	
		
			
			|  | 284 | +};
 |