iPXE
disklog.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 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 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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <assert.h>
28#include <errno.h>
29#include <string.h>
30#include <ipxe/disklog.h>
31
32/** @file
33 *
34 * Disk log console
35 *
36 */
37
38/**
39 * Open disk log console
40 *
41 * @v disklog Disk log
42 * @ret rc Return status code
43 *
44 * The data buffer must already contain the initial logical block.
45 */
46int disklog_open ( struct disklog *disklog ) {
47 struct disklog_header *hdr =
48 ( ( struct disklog_header * ) disklog->buffer );
49
50 /* Sanity checks */
51 assert ( disklog->console != NULL );
52 assert ( disklog->buffer != NULL );
53 assert ( disklog->blksize > 0 );
54 assert ( ( disklog->blksize & ( disklog->blksize - 1 ) ) == 0 );
56 assert ( disklog->op != NULL );
57 assert ( disklog->op->write != NULL );
58
59 /* Check magic signature */
60 if ( ( disklog->blksize < sizeof ( *hdr ) ) ||
61 ( memcmp ( hdr->magic, DISKLOG_MAGIC,
62 sizeof ( hdr->magic ) ) != 0 ) ) {
63 DBGC ( disklog, "DISKLOG has bad magic signature\n" );
64 return -EINVAL;
65 }
66
67 /* Initialise buffer */
68 disklog->offset = sizeof ( *hdr );
69 disklog->unwritten = 0;
70 memset ( ( disklog->buffer + sizeof ( *hdr ) ), 0,
71 ( disklog->blksize - sizeof ( *hdr ) ) );
72
73 /* Enable console */
75
76 return 0;
77}
78
79/**
80 * Write character to disk log console
81 *
82 * @v disklog Disk log
83 * @v character Character
84 */
85void disklog_putchar ( struct disklog *disklog, int character ) {
86 static int busy;
87 int rc;
88
89 /* Ignore if we are already mid-logging */
90 if ( busy )
91 return;
92 busy = 1;
93
94 /* Sanity checks */
96
97 /* Write character to buffer */
98 disklog->buffer[disklog->offset++] = character;
100
101 /* Write sector to disk, if applicable */
102 if ( ( disklog->offset == disklog->blksize ) ||
104 ( character == '\n' ) ) {
105
106 /* Write sector to disk */
107 if ( ( rc = disklog->op->write() ) != 0 ) {
108 DBGC ( disklog, "DISKLOG could not write: %s\n",
109 strerror ( rc ) );
110 /* Ignore and continue; there's nothing we can do */
111 }
112
113 /* Reset count of unwritten characters */
114 disklog->unwritten = 0;
115 }
116
117 /* Move to next sector, if applicable */
118 if ( disklog->offset == disklog->blksize ) {
119
120 /* Disable console if we have run out of space */
121 if ( disklog->lba >= disklog->max_lba )
123
124 /* Clear log buffer */
126 disklog->offset = 0;
127
128 /* Move to next sector */
129 disklog->lba++;
130 }
131
132 /* Clear busy flag */
133 busy = 0;
134}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
struct golan_inbox_hdr hdr
Message header.
Definition CIB_PRM.h:0
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
Assertions.
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
int disklog_open(struct disklog *disklog)
Open disk log console.
Definition disklog.c:46
void disklog_putchar(struct disklog *disklog, int character)
Write character to disk log console.
Definition disklog.c:85
Disk log console.
#define DISKLOG_MAX_UNWRITTEN
Maximum number of outstanding unwritten characters.
Definition disklog.h:90
#define DISKLOG_MAGIC
Disk log partition magic signature.
Definition disklog.h:87
Error codes.
#define DBGC(...)
Definition compiler.h:530
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define EINVAL
Invalid argument.
Definition errno.h:429
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
String functions.
void * memset(void *dest, int character, size_t len) __nonnull
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
int disabled
Console disabled flags.
Definition console.h:63
Disk log partition header.
Definition disklog.h:19
int(* write)(void)
Write current logical block.
Definition disklog.h:51
A disk log.
Definition disklog.h:25
struct disklog_operations * op
Disk log operations.
Definition disklog.h:29
uint8_t * buffer
Logical block data buffer.
Definition disklog.h:31
size_t blksize
Logical block size.
Definition disklog.h:33
unsigned int offset
Current offset within logical block.
Definition disklog.h:39
uint64_t lba
Current logical block index.
Definition disklog.h:35
unsigned int unwritten
Current number of unwritten characters.
Definition disklog.h:41
struct console_driver * console
Console device.
Definition disklog.h:27
uint64_t max_lba
Maximum logical block index.
Definition disklog.h:37