Skip to contentSkip to conversation

x86: Fix earlyprintk ttyS2/3

PATCH In review Daniel J Blueman 3 participants 4 messages Apr 7, 2015 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: Fix earlyprintk ttyS2/3

Quite a few platforms use ttyS2 for their serial-over-LAN, so fix early printk support for ttyS2 and 3, avoiding the need to hard-code the IO port.

---
 arch/x86/boot/early_serial_console.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/boot/early_serial_console.c +5 -4
index 5df2869..4f4c2e6 100644
@@ -71,15 +71,16 @@ static void parse_earlyprintk(void)
 			else
 				pos = e - arg;
 		} else if (!strncmp(arg + pos, "ttyS", 4)) {
-			static const int bases[] = { 0x3f8, 0x2f8 };
-			int idx = 0;
+			static const int bases[] = {0x3f8, 0x2f8, 0x3e8, 0x2e8};
+			unsigned idx = 0;
 
 			if (!strncmp(arg + pos, "ttyS", 4))
 				pos += 4;
 
-			if (arg[pos++] == '1')
-				idx = 1;
+			if ((arg[pos] - '0') < (sizeof(bases) / sizeof(bases[0])))
+				idx = arg[pos] - '0';
 
+			pos++;
 			port = bases[idx];
 		}
 
Signature · 1 line
2.1.0
#1 lore raw
Quoted 2 lines · Daniel J Blueman

Quite a few platforms use ttyS2 for their serial-over-LAN, so fix early printk support for ttyS2 and 3, avoiding the need to hard-code the IO port.

[]

arch/x86/boot/early_serial_console.c

Quoted 1 line
arch/x86/boot/early_serial_console.c +0 -0

[]

Quoted 6 lines
arch/x86/boot/early_serial_console.c +0 -2
@@ -71,15 +71,16 @@ static void parse_earlyprintk(void)
 			else
 				pos = e - arg;
 		} else if (!strncmp(arg + pos, "ttyS", 4)) {
-			static const int bases[] = { 0x3f8, 0x2f8 };
-			int idx = 0;

arch/x86/boot/early_serial_console.c static void parse_earlyprintk(void) · line 81

Quoted 10 lines
arch/x86/boot/early_serial_console.c +4 -2
+			static const int bases[] = {0x3f8, 0x2f8, 0x3e8, 0x2e8};
+			unsigned idx = 0;
 
 			if (!strncmp(arg + pos, "ttyS", 4))
 				pos += 4;
 
-			if (arg[pos++] == '1')
-				idx = 1;
+			if ((arg[pos] - '0') < (sizeof(bases) / sizeof(bases[0])))
+				idx = arg[pos] - '0';

This doesn't prevent negative indexing in case someone does something like shift key typo "ttyS!" for "ttyS1".

I've done that.

#2 lore raw
Quoted 26 lines · Joe Perches
Quoted 4 lines · Daniel J Blueman

Quite a few platforms use ttyS2 for their serial-over-LAN, so fix
early
printk support for ttyS2 and 3, avoiding the need to hard-code the
IO port.

[]

Quoted 2 lines

diff --git a/arch/x86/boot/early_serial_console.c
b/arch/x86/boot/early_serial_console.c

[]

Quoted 9 lines

@@ -71,15 +71,16 @@ static void parse_earlyprintk(void)
else
pos = e - arg;
} else if (!strncmp(arg + pos, "ttyS", 4)) {
- static const int bases[] = { 0x3f8, 0x2f8 };
- int idx = 0;
+ static const int bases[] = {0x3f8, 0x2f8, 0x3e8, 0x2e8};
+ unsigned idx = 0;

Quoted 7 lines
  			if (!strncmp(arg + pos, "ttyS", 4))
  				pos += 4;

- if (arg[pos++] == '1')
- idx = 1;
+ if ((arg[pos] - '0') < (sizeof(bases) / sizeof(bases[0])))
+ idx = arg[pos] - '0';

Quoted 4 lines

This doesn't prevent negative indexing in case someone does something like shift key typo "ttyS!" for "ttyS1".

I've done that.

Gah; late night coding syndrome. Adding the missing cast in the first case only:

Subject: [PATCH v2] x86: Fix earlyprintk ttyS2/3

Quite a few platforms use ttyS2 for their serial-over-LAN, so fix early printk support for ttyS2 and 3, avoiding the need to hard-code the IO port.

v2: Add missing unsigned cast to correct range check

---
arch/x86/boot/early_serial_console.c | 7 ++++---

1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/boot/early_serial_console.c +0 -0
index 5df2869..3d7f112 100644
@@ -71,15 +71,16 @@ static void parse_earlyprintk(void)
			else
				pos = e - arg;
		} else if (!strncmp(arg + pos, "ttyS", 4)) {
-			static const int bases[] = { 0x3f8, 0x2f8 };
+			static const int bases[] = {0x3f8, 0x2f8, 0x3e8, 0x2e8};
			int idx = 0;

			if (!strncmp(arg + pos, "ttyS", 4))
				pos += 4;

- if (arg[pos++] == '1')
- idx = 1;
+ if ((unsigned)(arg[pos] - '0') < (sizeof(bases) / sizeof(bases[0])))
+ idx = arg[pos] - '0';

+			pos++;
			port = bases[idx];
		}
#3 lore raw
In reply to Daniel J Blueman
Quoted 2 lines · Daniel J Blueman

Quite a few platforms use ttyS2 for their serial-over-LAN, so fix early printk support for ttyS2 and 3, avoiding the need to hard-code the IO port.

Nice.

Quoted 11 lines
---
 arch/x86/boot/early_serial_console.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/boot/early_serial_console.c +0 -0
index 5df2869..4f4c2e6 100644

arch/x86/boot/early_serial_console.c static void parse_earlyprintk(void) · line 78

Quoted 10 lines
arch/x86/boot/early_serial_console.c +2 -2
@@ -71,15 +71,16 @@ static void parse_earlyprintk(void)
 			else
 				pos = e - arg;
 		} else if (!strncmp(arg + pos, "ttyS", 4)) {
-			static const int bases[] = { 0x3f8, 0x2f8 };
-			int idx = 0;
+			static const int bases[] = {0x3f8, 0x2f8, 0x3e8, 0x2e8};
+			unsigned idx = 0;
 
 			if (!strncmp(arg + pos, "ttyS", 4))
 				pos += 4;

Btw., that's a weird pattern: why do the strncmp() twice? We already know that we matched, in this branch. Could be written as:

			pos += 4;

?

arch/x86/boot/early_serial_console.c static void parse_earlyprintk(void) · line 81

Quoted 5 lines
arch/x86/boot/early_serial_console.c +2 -2
-			if (arg[pos++] == '1')
-				idx = 1;
+			if ((arg[pos] - '0') < (sizeof(bases) / sizeof(bases[0])))
+				idx = arg[pos] - '0';

Nit: use ARRAY_SIZE()?

Not so nit: with your change, a typo in boot parameters like "ttyS-" would underflow the index and dereference randomly around bases[] with a negative index:

Also, higher indices like "ttyS4" will overflow beyond the end of the array.

Please add proper bounds checking and warning messages.

arch/x86/boot/early_serial_console.c static void parse_earlyprintk(void) · line 85

Quoted 4 lines
arch/x86/boot/early_serial_console.c +1 -0
+			pos++;
 			port = bases[idx];
 		}

Thanks,

Ingo