2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 #include <ipxe/console.h>
30 #include <ipxe/init.h>
33 #include <config/console.h>
37 * INT13 disk log console
41 /* Set default console usage if applicable */
42 #if ! ( defined ( CONSOLE_INT13 ) && CONSOLE_EXPLICIT ( CONSOLE_INT13 ) )
44 #define CONSOLE_INT13 ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
47 /** Disk drive number */
48 #define INT13CON_DRIVE 0x80
50 /** Log partition type */
51 #define INT13CON_PARTITION_TYPE 0xe0
53 /** Maximum number of outstanding unwritten characters */
54 #define INT13CON_MAX_UNWRITTEN 64
56 /** Log partition header */
57 struct int13con_header {
58 /** Magic signature */
60 } __attribute__ (( packed ));
62 /** Log partition magic signature */
63 #define INT13CON_MAGIC "iPXE LOG\n\n"
66 static uint8_t __bss16_array ( int13con_buffer, [INT13_BLKSIZE] );
67 #define int13con_buffer __use_data16 ( int13con_buffer )
69 /** Disk address packet */
70 static struct int13_disk_address __bss16 ( int13con_address );
71 #define int13con_address __use_data16 ( int13con_address )
74 static uint64_t int13con_lba;
77 static uint64_t int13con_max_lba;
79 /** Current offset within sector */
80 static size_t int13con_offset;
82 /** Number of unwritten characters */
83 static size_t int13con_unwritten;
85 struct console_driver int13con __console_driver;
88 * Read/write disk sector
91 * @v lba Logical block address
92 * @ret rc Return status code
94 static int int13con_rw ( unsigned int op, uint64_t lba ) {
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;
105 __asm__ ( REAL_CODE ( "int $0x13\n\t" )
107 : "0" ( op << 8 ), "d" ( INT13CON_DRIVE ),
108 "S" ( __from_data16 ( &int13con_address ) ) );
110 DBG ( "INT13CON operation %04x failed: %02x\n",
119 * Write character to console
121 * @v character Character
123 static void int13con_putchar ( int character ) {
127 /* Ignore if we are already mid-logging */
132 /* Write character to buffer */
133 int13con_buffer[int13con_offset++] = character;
134 int13con_unwritten++;
136 /* Write sector to disk, if applicable */
137 if ( ( int13con_offset == INT13_BLKSIZE ) ||
138 ( int13con_unwritten == INT13CON_MAX_UNWRITTEN ) ||
139 ( character == '\n' ) ) {
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 */
148 /* Reset count of unwritten characters */
149 int13con_unwritten = 0;
152 /* Move to next sector, if applicable */
153 if ( int13con_offset == INT13_BLKSIZE ) {
155 /* Disable console if we have run out of space */
156 if ( int13con_lba >= int13con_max_lba )
157 int13con.disabled = 1;
159 /* Clear log buffer */
160 memset ( int13con_buffer, 0, sizeof ( int13con_buffer ) );
163 /* Move to next sector */
167 /* Clear busy flag */
174 * @ret rc Return status code
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];
186 if ( ( rc = int13con_rw ( INT13_EXTENDED_READ, 0 ) ) != 0 ) {
187 DBG ( "INT13CON could not read MBR: %s\n", strerror ( rc ) );
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 ) );
198 /* Look for magic partition */
199 memcpy ( part, mbr->partitions, sizeof ( part ) );
200 for ( i = 0 ; i < ( sizeof ( part ) / sizeof ( part[0] ) ) ; i++ ) {
202 /* Skip partitions of the wrong type */
203 if ( part[i].type != INT13CON_PARTITION_TYPE )
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 ) );
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 ) );
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 );
228 /* Initialise log buffer */
229 memset ( &int13con_buffer[ sizeof ( *hdr ) ], 0,
230 ( sizeof ( int13con_buffer ) - sizeof ( *hdr ) ) );
231 int13con_offset = sizeof ( hdr->magic );
236 DBG ( "INT13CON found no log partition\n" );
241 * Initialise INT13 console
244 static void int13con_init ( void ) {
247 unsigned int discard_c;
248 unsigned int discard_d;
251 /* Check for INT13 extensions */
252 __asm__ __volatile__ ( REAL_CODE ( "int $0x13\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",
264 /* Locate log partition */
265 if ( ( rc = int13con_find() ) != 0)
269 int13con.disabled = 0;
273 * INT13 console initialisation function
275 struct init_fn int13con_init_fn __init_fn ( INIT_CONSOLE ) = {
276 .initialise = int13con_init,
279 /** INT13 console driver */
280 struct console_driver int13con __console_driver = {
281 .putchar = int13con_putchar,
282 .disabled = CONSOLE_DISABLED,
283 .usage = CONSOLE_INT13,