iPXE
int13con.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <string.h>
00028 #include <errno.h>
00029 #include <ipxe/console.h>
00030 #include <ipxe/init.h>
00031 #include <realmode.h>
00032 #include <int13.h>
00033 #include <config/console.h>
00034 
00035 /** @file
00036  *
00037  * INT13 disk log console
00038  *
00039  */
00040 
00041 /* Set default console usage if applicable */
00042 #if ! ( defined ( CONSOLE_INT13 ) && CONSOLE_EXPLICIT ( CONSOLE_INT13 ) )
00043 #undef CONSOLE_INT13
00044 #define CONSOLE_INT13 ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
00045 #endif
00046 
00047 /** Disk drive number */
00048 #define INT13CON_DRIVE 0x80
00049 
00050 /** Log partition type */
00051 #define INT13CON_PARTITION_TYPE 0xe0
00052 
00053 /** Maximum number of outstanding unwritten characters */
00054 #define INT13CON_MAX_UNWRITTEN 64
00055 
00056 /** Log partition header */
00057 struct int13con_header {
00058         /** Magic signature */
00059         char magic[10];
00060 } __attribute__ (( packed ));
00061 
00062 /** Log partition magic signature */
00063 #define INT13CON_MAGIC "iPXE LOG\n\n"
00064 
00065 /** Original INT13 vector */
00066 static struct segoff __bss16 ( int13con_vector );
00067 #define int13con_vector __use_data16 ( int13con_vector )
00068 
00069 /** Sector buffer */
00070 static uint8_t __bss16_array ( int13con_buffer, [INT13_BLKSIZE] );
00071 #define int13con_buffer __use_data16 ( int13con_buffer )
00072 
00073 /** Disk address packet */
00074 static struct int13_disk_address __bss16 ( int13con_address );
00075 #define int13con_address __use_data16 ( int13con_address )
00076 
00077 /** Current LBA */
00078 static uint64_t int13con_lba;
00079 
00080 /** Maximum LBA */
00081 static uint64_t int13con_max_lba;
00082 
00083 /** Current offset within sector */
00084 static size_t int13con_offset;
00085 
00086 /** Number of unwritten characters */
00087 static size_t int13con_unwritten;
00088 
00089 struct console_driver int13con __console_driver;
00090 
00091 /**
00092  * Read/write disk sector
00093  *
00094  * @v op                Operation
00095  * @v lba               Logical block address
00096  * @ret rc              Return status code
00097  */
00098 static int int13con_rw ( unsigned int op, uint64_t lba ) {
00099         uint8_t error;
00100 
00101         /* Construct disk address packet */
00102         int13con_address.bufsize = sizeof ( int13con_address );
00103         int13con_address.count = 1;
00104         int13con_address.buffer.segment = rm_ds;
00105         int13con_address.buffer.offset = __from_data16 ( int13con_buffer );
00106         int13con_address.lba = lba;
00107 
00108         /* Emulate INT13 via original vector.  We do this since iPXE
00109          * (or another subsequent bootloader) may hook INT13 and remap
00110          * drive numbers.
00111          */
00112         __asm__ ( REAL_CODE ( "pushfw\n\t"
00113                               "cli\n\t"
00114                               "lcall *int13con_vector\n\t" )
00115                   : "=a" ( error )
00116                   : "0" ( op << 8 ), "d" ( INT13CON_DRIVE ),
00117                     "S" ( __from_data16 ( &int13con_address ) ) );
00118         if ( error ) {
00119                 DBG ( "INT13CON operation %04x failed: %02x\n",
00120                       op, error );
00121                 return -EIO;
00122         }
00123 
00124         return 0;
00125 }
00126 
00127 /**
00128  * Write character to console
00129  *
00130  * @v character         Character
00131  */
00132 static void int13con_putchar ( int character ) {
00133         static int busy;
00134         int rc;
00135 
00136         /* Ignore if we are already mid-logging */
00137         if ( busy )
00138                 return;
00139         busy = 1;
00140 
00141         /* Write character to buffer */
00142         int13con_buffer[int13con_offset++] = character;
00143         int13con_unwritten++;
00144 
00145         /* Write sector to disk, if applicable */
00146         if ( ( int13con_offset == INT13_BLKSIZE ) ||
00147              ( int13con_unwritten == INT13CON_MAX_UNWRITTEN ) ||
00148              ( character == '\n' ) ) {
00149 
00150                 /* Write sector to disk */
00151                 if ( ( rc = int13con_rw ( INT13_EXTENDED_WRITE,
00152                                           int13con_lba ) ) != 0 ) {
00153                         DBG ( "INT13CON could not write log\n" );
00154                         /* Ignore and continue; there's nothing we can do */
00155                 }
00156 
00157                 /* Reset count of unwritten characters */
00158                 int13con_unwritten = 0;
00159         }
00160 
00161         /* Move to next sector, if applicable */
00162         if ( int13con_offset == INT13_BLKSIZE ) {
00163 
00164                 /* Disable console if we have run out of space */
00165                 if ( int13con_lba >= int13con_max_lba )
00166                         int13con.disabled = 1;
00167 
00168                 /* Clear log buffer */
00169                 memset ( int13con_buffer, 0, sizeof ( int13con_buffer ) );
00170                 int13con_offset = 0;
00171 
00172                 /* Move to next sector */
00173                 int13con_lba++;
00174         }
00175 
00176         /* Clear busy flag */
00177         busy = 0;
00178 }
00179 
00180 /**
00181  * Find log partition
00182  *
00183  * @ret rc              Return status code
00184  */
00185 static int int13con_find ( void ) {
00186         struct master_boot_record *mbr =
00187                 ( ( struct master_boot_record * ) int13con_buffer );
00188         struct int13con_header *hdr =
00189                 ( ( struct int13con_header * ) int13con_buffer );
00190         struct partition_table_entry part[4];
00191         unsigned int i;
00192         int rc;
00193 
00194         /* Read MBR */
00195         if ( ( rc = int13con_rw ( INT13_EXTENDED_READ, 0 ) ) != 0 ) {
00196                 DBG ( "INT13CON could not read MBR: %s\n", strerror ( rc ) );
00197                 return rc;
00198         }
00199 
00200         /* Check MBR magic */
00201         if ( mbr->magic != INT13_MBR_MAGIC ) {
00202                 DBG ( "INT13CON incorrect MBR magic\n" );
00203                 DBG2_HDA ( 0, mbr, sizeof ( *mbr ) );
00204                 return -EINVAL;
00205         }
00206 
00207         /* Look for magic partition */
00208         memcpy ( part, mbr->partitions, sizeof ( part ) );
00209         for ( i = 0 ; i < ( sizeof ( part ) / sizeof ( part[0] ) ) ; i++ ) {
00210 
00211                 /* Skip partitions of the wrong type */
00212                 if ( part[i].type != INT13CON_PARTITION_TYPE )
00213                         continue;
00214 
00215                 /* Read partition header */
00216                 if ( ( rc = int13con_rw ( INT13_EXTENDED_READ,
00217                                           part[i].start ) ) != 0 ) {
00218                         DBG ( "INT13CON partition %d could not read header: "
00219                               "%s\n", ( i + 1 ), strerror ( rc ) );
00220                         continue;
00221                 }
00222 
00223                 /* Check partition header */
00224                 if ( memcmp ( hdr->magic, INT13CON_MAGIC,
00225                               sizeof ( hdr->magic ) ) != 0 ) {
00226                         DBG ( "INT13CON partition %d bad magic\n", ( i + 1 ) );
00227                         DBG2_HDA ( 0, hdr, sizeof ( *hdr ) );
00228                         continue;
00229                 }
00230 
00231                 /* Found log partition */
00232                 DBG ( "INT13CON partition %d at [%08x,%08x)\n", ( i + 1 ),
00233                       part[i].start, ( part[i].start + part[i].length ) );
00234                 int13con_lba = part[i].start;
00235                 int13con_max_lba = ( part[i].start + part[i].length - 1 );
00236 
00237                 /* Initialise log buffer */
00238                 memset ( &int13con_buffer[ sizeof ( *hdr ) ], 0,
00239                          ( sizeof ( int13con_buffer ) - sizeof ( *hdr ) ) );
00240                 int13con_offset = sizeof ( hdr->magic );
00241 
00242                 return 0;
00243         }
00244 
00245         DBG ( "INT13CON found no log partition\n" );
00246         return -ENOENT;
00247 }
00248 
00249 /**
00250  * Initialise INT13 console
00251  *
00252  */
00253 static void int13con_init ( void ) {
00254         uint8_t error;
00255         uint16_t check;
00256         unsigned int discard_c;
00257         unsigned int discard_d;
00258         int rc;
00259 
00260         /* Check for INT13 extensions */
00261         __asm__ __volatile__ ( REAL_CODE ( "int $0x13\n\t"
00262                                            "setc %%al\n\t" )
00263                                : "=a" ( error ), "=b" ( check ),
00264                                  "=c" ( discard_c ), "=d" ( discard_d )
00265                                : "0" ( INT13_EXTENSION_CHECK << 8 ),
00266                                  "1" ( 0x55aa ), "3" ( INT13CON_DRIVE ) );
00267         if ( error || ( check != 0xaa55 ) ) {
00268                 DBG ( "INT13CON missing extensions (%02x,%04x)\n",
00269                       error, check );
00270                 return;
00271         }
00272 
00273         /* Store original INT13 vector */
00274         copy_from_real ( &int13con_vector, 0, ( 0x13 * 4 ),
00275                          sizeof ( int13con_vector ) );
00276         DBG ( "INT13CON using original INT13 vector %04x:%04x\n",
00277               int13con_vector.segment, int13con_vector.offset );
00278 
00279         /* Locate log partition */
00280         if ( ( rc = int13con_find() ) != 0)
00281                 return;
00282 
00283         /* Enable console */
00284         int13con.disabled = 0;
00285 }
00286 
00287 /**
00288  * INT13 console initialisation function
00289  */
00290 struct init_fn int13con_init_fn __init_fn ( INIT_CONSOLE ) = {
00291         .initialise = int13con_init,
00292 };
00293 
00294 /** INT13 console driver */
00295 struct console_driver int13con __console_driver = {
00296         .putchar = int13con_putchar,
00297         .disabled = CONSOLE_DISABLED,
00298         .usage = CONSOLE_INT13,
00299 };