Skip to content

Commit 9ba8ec8

Browse files
H. Peter Anvinbp3tk0v
authored andcommitted
x86/boot: Add error_putdec() helper
Add a helper to print decimal numbers to early console. Suggested-by: Borislav Petkov (AMD) <bp@alien8.de> Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com> Signed-off-by: Jun'ichi Nomura <junichi.nomura@nec.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Link: https://lore.kernel.org/lkml/20240123112624.GBZa-iYP1l9SSYtr-V@fat_crate.local/ Link: https://lore.kernel.org/r/20240202035052.17963-1-junichi.nomura@nec.com
1 parent 1567570 commit 9ba8ec8

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

arch/x86/boot/compressed/misc.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,21 +164,34 @@ void __putstr(const char *s)
164164
outb(0xff & (pos >> 1), vidport+1);
165165
}
166166

167-
void __puthex(unsigned long value)
167+
static noinline void __putnum(unsigned long value, unsigned int base,
168+
int mindig)
168169
{
169-
char alpha[2] = "0";
170-
int bits;
170+
char buf[8*sizeof(value)+1];
171+
char *p;
171172

172-
for (bits = sizeof(value) * 8 - 4; bits >= 0; bits -= 4) {
173-
unsigned long digit = (value >> bits) & 0xf;
173+
p = buf + sizeof(buf);
174+
*--p = '\0';
174175

175-
if (digit < 0xA)
176-
alpha[0] = '0' + digit;
177-
else
178-
alpha[0] = 'a' + (digit - 0xA);
176+
while (mindig-- > 0 || value) {
177+
unsigned char digit = value % base;
178+
digit += (digit >= 10) ? ('a'-10) : '0';
179+
*--p = digit;
179180

180-
__putstr(alpha);
181+
value /= base;
181182
}
183+
184+
__putstr(p);
185+
}
186+
187+
void __puthex(unsigned long value)
188+
{
189+
__putnum(value, 16, sizeof(value)*2);
190+
}
191+
192+
void __putdec(unsigned long value)
193+
{
194+
__putnum(value, 10, 1);
182195
}
183196

184197
#ifdef CONFIG_X86_NEED_RELOCS

arch/x86/boot/compressed/misc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ void *malloc(int size);
6363
void free(void *where);
6464
void __putstr(const char *s);
6565
void __puthex(unsigned long value);
66+
void __putdec(unsigned long value);
6667
#define error_putstr(__x) __putstr(__x)
6768
#define error_puthex(__x) __puthex(__x)
69+
#define error_putdec(__x) __putdec(__x)
6870

6971
#ifdef CONFIG_X86_VERBOSE_BOOTUP
7072

0 commit comments

Comments
 (0)