Skip to contentSkip to conversation

Interactive Boot Menu for DASD and SCSI Guests on s390x

PATCH Superseded Collin L. Walling 5 participants 23 messages Dec 11, 2017 – Jan 9, 2018

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v2 cover Interactive Boot Menu for DASD and SCSI Guests on s390x

Thanks for your patience. I've been bouncing back-and-forth between different designs.

Lots-o-changes this version. Please call me out if I missed anything from the previous round of review.

--- [v2] ---

libc

- fixed up atoi and itostr and moved them to the new lib.c file

- documentation follows gtk-doc (let me know if I'm missing something)

ipl structs

- fixed up commit message

- s/BootEckd*/Eckd*

boot option parsing

- hw/s390x/ipl.c now handles *all* logic behind parsing command line values and setting the appropriate values in the iplb (including interpreting loadparm)

- pc-bios/s390-ccw/main.c now only sets the boot menu fields that were read from the iplb (no longer interpreting loadparm here)

- timeout value is now stored as seconds instead of milliseconds (maximum of 65535 seconds [~18 hours])

- error reported for invalid splash-time value
- if splash-time is invalid, then set it to 0 (wait forever)
- if splash-time is greater than max, then set it to max

- s/boot_menu_enabled/boot_menu_flags

- boot_menu_flags is set to *one* of these flags:
- BOOT_MENU_FLAG_BOOT_OPTS
- set if -boot menu=on or -machine loadparm=prompt
- BOOT_MENU_FLAG_ZIPL_OPTS
- set if no boot options or loadparm are set

- fixed ordering of the new fields in the iplb's

boot menu for eckd dasd

- now supports zipl loader values

- function chs removed and now using pre-existing function eckd_block_num instead

- sclp_read functionality and its helpers are now in menu.c, which is where the only call to this function occurs
- renamed to read_prompt
- renamed read in sclp.c to sclp_read

- introduced header menu.h

- introduced new struct, ZiplParms that contains the following fields relating to zipl boot menu data:
- flags
- timeout
- menu_start

- stage2 reading cleaned up

- no longer panic if boot menu data is not found -- instead just print a message and boot default (what if the user did not configure a menu?)

boot menu for scsi

- will only show a menu if BOOT_MENU_FLAG_BOOT_OPTS was set

--- [Summary] ---

These patches implement a boot menu for ECKD DASD and SCSI guests on s390x. The menu will only appear if the disk has been configured for IPL with the zIPL tool and with the following QEMU command line options:

-boot menu=on[,splash-time=X] and/or -machine loadparm='prompt'

or via the following libvirt domain xml:

    <os>
      <bootmenu enable='yes' timeout='X'/>
    </os>

    or
	
    <disk>
      ...
      <boot order='1' loadparm='PROMPT'/>
    </disk>

Where X is some positive integer representing time in milliseconds.

A loadparm other than 'prompt' will disable the menu and just boot the specified entry.

If no boot options are specified, we will attempt to use the values provided by zipl (ECKD DASD only).

Collin L. Walling (5):
s390-ccw: update libc
s390-ccw: ipl structs for eckd cdl/ldl
s390-ccw: parse and set boot menu options
s390-ccw: interactive boot menu for eckd dasd
s390-ccw: interactive boot menu for scsi

 hw/s390x/ipl.c              |  55 ++++++++++
 hw/s390x/ipl.h              |   8 +-
 pc-bios/s390-ccw/Makefile   |   2 +-
 pc-bios/s390-ccw/bootmap.c  | 104 +++++++++++++++----
 pc-bios/s390-ccw/bootmap.h  |  73 ++++++++------
 pc-bios/s390-ccw/iplb.h     |   8 +-
 pc-bios/s390-ccw/libc.c     |  75 ++++++++++++++
 pc-bios/s390-ccw/libc.h     |  31 ++++++
 pc-bios/s390-ccw/main.c     |  22 ++--
 pc-bios/s390-ccw/menu.c     | 237 ++++++++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/menu.h     |  29 ++++++
 pc-bios/s390-ccw/s390-ccw.h |   2 +
 pc-bios/s390-ccw/sclp.c     |  30 ++++--
 pc-bios/s390-ccw/virtio.c   |   2 +-
 14 files changed, 600 insertions(+), 78 deletions(-)
 create mode 100644 pc-bios/s390-ccw/libc.c
 create mode 100644 pc-bios/s390-ccw/menu.c
 create mode 100644 pc-bios/s390-ccw/menu.h
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/5 s390-ccw: update libc

Moved:
memcmp from bootmap.h to libc.h (renamed from _memcmp) strlen from sclp.c to libc.h (renamed from _strlen)

Added C standard functions:
isdigit
atoi

Added non-C standard function:
itostr

---
 pc-bios/s390-ccw/Makefile  |  2 +-
 pc-bios/s390-ccw/bootmap.c |  4 +--
 pc-bios/s390-ccw/bootmap.h | 16 +---------
 pc-bios/s390-ccw/libc.c    | 75 ++++++++++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/libc.h    | 31 +++++++++++++++++++
 pc-bios/s390-ccw/main.c    | 17 +----------
 pc-bios/s390-ccw/sclp.c    | 10 +------
 7 files changed, 112 insertions(+), 43 deletions(-)
 create mode 100644 pc-bios/s390-ccw/libc.c
pc-bios/s390-ccw/Makefile +1 -1
index 6d0c2ee..9f7904f 100644
@@ -9,7 +9,7 @@ $(call set-vpath, $(SRC_PATH)/pc-bios/s390-ccw)
 
 .PHONY : all clean build-all
 
-OBJECTS = start.o main.o bootmap.o sclp.o virtio.o virtio-scsi.o virtio-blkdev.o
+OBJECTS = start.o main.o bootmap.o sclp.o virtio.o virtio-scsi.o virtio-blkdev.o libc.o
 QEMU_CFLAGS := $(filter -W%, $(QEMU_CFLAGS))
 QEMU_CFLAGS += -ffreestanding -fno-delete-null-pointer-checks -msoft-float
 QEMU_CFLAGS += -march=z900 -fPIE -fno-strict-aliasing
pc-bios/s390-ccw/bootmap.c +2 -2
index 67a6123..6f8e30f 100644
@@ -512,7 +512,7 @@ static bool is_iso_bc_entry_compatible(IsoBcSection *s)
                     "Failed to read image sector 0");
 
     /* Checking bytes 8 - 32 for S390 Linux magic */
-    return !_memcmp(magic_sec + 8, linux_s390_magic, 24);
+    return !memcmp(magic_sec + 8, linux_s390_magic, 24);
 }
 
 /* Location of the current sector of the directory */
@@ -641,7 +641,7 @@ static uint32_t find_iso_bc(void)
         if (vd->type == VOL_DESC_TYPE_BOOT) {
             IsoVdElTorito *et = &vd->vd.boot;
 
-            if (!_memcmp(&et->el_torito[0], el_torito_magic, 32)) {
+            if (!memcmp(&et->el_torito[0], el_torito_magic, 32)) {
                 return bswap32(et->bc_offset);
             }
         }
pc-bios/s390-ccw/bootmap.h +1 -15
index cf99a4c..4980838 100644
@@ -310,20 +310,6 @@ static inline bool magic_match(const void *data, const void *magic)
     return *((uint32_t *)data) == *((uint32_t *)magic);
 }
 
-static inline int _memcmp(const void *s1, const void *s2, size_t n)
-{
-    int i;
-    const uint8_t *p1 = s1, *p2 = s2;
-
-    for (i = 0; i < n; i++) {
-        if (p1[i] != p2[i]) {
-            return p1[i] > p2[i] ? 1 : -1;
-        }
-    }
-
-    return 0;
-}
-
 static inline uint32_t iso_733_to_u32(uint64_t x)
 {
     return (uint32_t)x;
@@ -416,7 +402,7 @@ const uint8_t vol_desc_magic[] = "CD001";
 
 static inline bool is_iso_vd_valid(IsoVolDesc *vd)
 {
-    return !_memcmp(&vd->ident[0], vol_desc_magic, 5) &&
+    return !memcmp(&vd->ident[0], vol_desc_magic, 5) &&
            vd->version == 0x1 &&
            vd->type <= VOL_DESC_TYPE_PARTITION;
 }
pc-bios/s390-ccw/libc.c new file +75 -0

75 lines not on this page · raw message

pc-bios/s390-ccw/libc.h +31 -0
index 0142ea8..00268e3 100644
@@ -42,4 +42,35 @@ static inline void *memcpy(void *s1, const void *s2, size_t n)
     return s1;
 }
 
+static inline int memcmp(const void *s1, const void *s2, size_t n)
+{
+    int i;
+    const uint8_t *p1 = s1, *p2 = s2;
+
+    for (i = 0; i < n; i++) {
+        if (p1[i] != p2[i]) {
+            return p1[i] > p2[i] ? 1 : -1;
+        }
+    }
+
+    return 0;
+}
+
+static inline size_t strlen(const char *str)
+{
+    size_t i;
+    for (i = 0; *str; i++) {
+        str++;
+    }
+    return i;
+}
+
+static inline int isdigit(int c)
+{
+    return (c >= '0') && (c <= '9');
+}
+
+int atoi(const char *str);
+char *itostr(int num, char *str, size_t len);
+
 #endif
pc-bios/s390-ccw/main.c +1 -16
index 401e9db..a8ef120 100644
@@ -40,22 +40,7 @@ void panic(const char *string)
 
 unsigned int get_loadparm_index(void)
 {
-    const char *lp = loadparm;
-    int i;
-    unsigned int idx = 0;
-
-    for (i = 0; i < 8; i++) {
-        char c = lp[i];
-
-        if (c < '0' || c > '9') {
-            break;
-        }
-
-        idx *= 10;
-        idx += c - '0';
-    }
-
-    return idx;
+    return atoi(loadparm);
 }
 
 static bool find_dev(Schib *schib, int dev_no)
pc-bios/s390-ccw/sclp.c +1 -9
index b1fc8ff..486fce1 100644
@@ -65,14 +65,6 @@ void sclp_setup(void)
     sclp_set_write_mask();
 }
 
-static int _strlen(const char *str)
-{
-    int i;
-    for (i = 0; *str; i++)
-        str++;
-    return i;
-}
-
 long write(int fd, const void *str, size_t len)
 {
     WriteEventData *sccb = (void *)_sccb;
@@ -95,7 +87,7 @@ long write(int fd, const void *str, size_t len)
 
 void sclp_print(const char *str)
 {
-    write(1, str, _strlen(str));
+    write(1, str, strlen(str));
 }
 
 void sclp_get_loadparm_ascii(char *loadparm)
Signature · 1 line
2.7.4
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/5] s390-ccw: update libc

Quoted 24 lines · Collin L. Walling

Moved:
memcmp from bootmap.h to libc.h (renamed from _memcmp) strlen from sclp.c to libc.h (renamed from _strlen)

Added C standard functions:
isdigit
atoi

Added non-C standard function:
itostr

---
 pc-bios/s390-ccw/Makefile  |  2 +-
 pc-bios/s390-ccw/bootmap.c |  4 +--
 pc-bios/s390-ccw/bootmap.h | 16 +---------
 pc-bios/s390-ccw/libc.c    | 75 ++++++++++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/libc.h    | 31 +++++++++++++++++++
 pc-bios/s390-ccw/main.c    | 17 +----------
 pc-bios/s390-ccw/sclp.c    | 10 +------
 7 files changed, 112 insertions(+), 43 deletions(-)
 create mode 100644 pc-bios/s390-ccw/libc.c

[...]

Quoted 18 lines
pc-bios/s390-ccw/libc.c new file +18 -0
+
+/**
+ * itostr:
+ * @num: the integer to be converted.
+ * @str: a pointer to a string to store the conversion.
+ * @len: the length of the passed string.
+ *
+ * Given an integer @num, convert it to a string. The string @str must be
+ * allocated beforehand. The resulting string will be null terminated and
+ * returned.
+ *
+ * Returns: the string @str of the converted integer @num.
+ */
+char *itostr(int num, char *str, size_t len)
+{
+    long num_len = 1;
+    int tmp = num;
+    int i;

pc-bios/s390-ccw/libc.c line 64

Quoted 10 lines
pc-bios/s390-ccw/libc.c new file +10 -0
+
+    /* Count length of num */
+    while ((tmp /= 10) > 0) {
+        num_len++;
+    }
+
+    /* Check if we have enough space for num and null */
+    if (len < num_len) {
+        return 0;
+    }

I'm afraid, but I think you've got an off-by-one bug in this code.

In patch 5, you're using this function like this:

    char tmp[4];

    sclp_print(itostr(entries, tmp, sizeof(tmp)));

That means if entries is >= 1000 for example, num_len is 4 ...

pc-bios/s390-ccw/libc.c line 72

Quoted 7 lines
pc-bios/s390-ccw/libc.c new file +7 -0
+    /* Convert int to string */
+    for (i = num_len - 1; i >= 0; i--) {
+        str[i] = num % 10 + '0';
+        num /= 10;
+    }
+
+    str[num_len] = '\0';

... and then you run into a buffer overflow here.

pc-bios/s390-ccw/libc.c line 75

Quoted 2 lines
pc-bios/s390-ccw/libc.c new file +2 -0
+    return str;
+}

Maybe it would also make more sense to panic() instead of "return 0" since you don't check the return value in patch 5 ?

Thomas

#3 lore raw

Re: [Qemu-devel] [qemu-s390x] [PATCH v2 1/5] s390-ccw: update libc

Quoted 64 lines · Thomas Huth
Quoted 24 lines · Collin L. Walling

Moved:
memcmp from bootmap.h to libc.h (renamed from _memcmp) strlen from sclp.c to libc.h (renamed from _strlen)

Added C standard functions:
isdigit
atoi

Added non-C standard function:
itostr

---
  pc-bios/s390-ccw/Makefile  |  2 +-
  pc-bios/s390-ccw/bootmap.c |  4 +--
  pc-bios/s390-ccw/bootmap.h | 16 +---------
  pc-bios/s390-ccw/libc.c    | 75 ++++++++++++++++++++++++++++++++++++++++++++++
  pc-bios/s390-ccw/libc.h    | 31 +++++++++++++++++++
  pc-bios/s390-ccw/main.c    | 17 +----------
  pc-bios/s390-ccw/sclp.c    | 10 +------
  7 files changed, 112 insertions(+), 43 deletions(-)

create mode 100644 pc-bios/s390-ccw/libc.c

[...]

Quoted 18 lines
pc-bios/s390-ccw/libc.c new file +18 -0
+
+/**
+ * itostr:
+ * @num: the integer to be converted.
+ * @str: a pointer to a string to store the conversion.
+ * @len: the length of the passed string.
+ *
+ * Given an integer @num, convert it to a string. The string @str must be
+ * allocated beforehand. The resulting string will be null terminated and
+ * returned.
+ *
+ * Returns: the string @str of the converted integer @num.
+ */
+char *itostr(int num, char *str, size_t len)
+{
+    long num_len = 1;
+    int tmp = num;
+    int i;

pc-bios/s390-ccw/libc.c line 64

Quoted 10 lines
pc-bios/s390-ccw/libc.c new file +10 -0
+
+    /* Count length of num */
+    while ((tmp /= 10) > 0) {
+        num_len++;
+    }
+
+    /* Check if we have enough space for num and null */
+    if (len < num_len) {
+        return 0;
+    }

I'm afraid, but I think you've got an off-by-one bug in this code.

In patch 5, you're using this function like this:

     char tmp[4];

     sclp_print(itostr(entries, tmp, sizeof(tmp)));

That means if entries is >= 1000 for example, num_len is 4 ...

Quoted 8 lines

pc-bios/s390-ccw/libc.c line 72

Quoted 7 lines
pc-bios/s390-ccw/libc.c new file +7 -0
+    /* Convert int to string */
+    for (i = num_len - 1; i >= 0; i--) {
+        str[i] = num % 10 + '0';
+        num /= 10;
+    }
+
+    str[num_len] = '\0';

... and then you run into a buffer overflow here.

Doh, you're correct.  I forgot to put a "<=" in the len / num_len check. That should fix things up.  Thanks for catching that.

Quoted 5 lines

pc-bios/s390-ccw/libc.c line 75

Quoted 3 lines
pc-bios/s390-ccw/libc.c new file +2 -0
+    return str;
+}

Maybe it would also make more sense to panic() instead of "return 0" since you don't check the return value in patch 5 ?

I'm a bit conflicted about doing something like that.  I'm not sure if there's any kind of guideline we want to follow for defining functions in libc.

I see one of two possibilities:

a.  define these functions as "libc-like" as possible, and use them as if they were regular standard libc functions

    or

b.  change up these functions to better fit their use cases in pc-bios/s390-ccw

Does that make sense?  What do you think?

Quoted 3 lines

Thomas

Signature · 1 line
- Collin L Walling
#4 lore raw

Re: [Qemu-devel] [qemu-s390x] [PATCH v2 1/5] s390-ccw: update libc

Quoted 99 lines · Collin L. Walling
Quoted 67 lines · Thomas Huth
Quoted 25 lines · Collin L. Walling

Moved:
   memcmp from bootmap.h to libc.h (renamed from _memcmp) strlen from sclp.c to libc.h (renamed from _strlen)

Added C standard functions:
   isdigit
   atoi

Added non-C standard function:
   itostr

---
  pc-bios/s390-ccw/Makefile  |  2 +-
  pc-bios/s390-ccw/bootmap.c |  4 +--
  pc-bios/s390-ccw/bootmap.h | 16 +---------
  pc-bios/s390-ccw/libc.c    | 75
++++++++++++++++++++++++++++++++++++++++++++++

  pc-bios/s390-ccw/libc.h    | 31 +++++++++++++++++++
  pc-bios/s390-ccw/main.c    | 17 +----------
  pc-bios/s390-ccw/sclp.c    | 10 +------
  7 files changed, 112 insertions(+), 43 deletions(-)

  create mode 100644 pc-bios/s390-ccw/libc.c

[...]

Quoted 20 lines
pc-bios/s390-ccw/libc.c new file +8 -0
+
+/**
+ * itostr:
+ * @num: the integer to be converted.
+ * @str: a pointer to a string to store the conversion.
+ * @len: the length of the passed string.
+ *
+ * Given an integer @num, convert it to a string. The string @str

must be
+ * allocated beforehand. The resulting string will be null terminated and
+ * returned.
+ *
+ * Returns: the string @str of the converted integer @num.
+ */

pc-bios/s390-ccw/libc.c new file +5 -0
+char *itostr(int num, char *str, size_t len)
+{
+    long num_len = 1;
+    int tmp = num;
+    int i;

pc-bios/s390-ccw/libc.c line 64

Quoted 10 lines
pc-bios/s390-ccw/libc.c new file +10 -0
+
+    /* Count length of num */
+    while ((tmp /= 10) > 0) {
+        num_len++;
+    }
+
+    /* Check if we have enough space for num and null */
+    if (len < num_len) {
+        return 0;
+    }

I'm afraid, but I think you've got an off-by-one bug in this code.

In patch 5, you're using this function like this:

     char tmp[4];

     sclp_print(itostr(entries, tmp, sizeof(tmp)));

That means if entries is >= 1000 for example, num_len is 4 ...

Quoted 8 lines
Quoted 7 lines

+    /* Convert int to string */
+    for (i = num_len - 1; i >= 0; i--) {
+        str[i] = num % 10 + '0';
+        num /= 10;
+    }
+
+    str[num_len] = '\0';

... and then you run into a buffer overflow here.

Doh, you're correct.  I forgot to put a "<=" in the len / num_len check. That should fix things up.  Thanks for catching that.

Quoted 5 lines
Quoted 3 lines

+    return str;
+}

Maybe it would also make more sense to panic() instead of "return 0" since you don't check the return value in patch 5 ?

I'm a bit conflicted about doing something like that.  I'm not sure if there's any kind
of guideline we want to follow for defining functions in libc.

I see one of two possibilities:

a.  define these functions as "libc-like" as possible, and use them as if they were
     regular standard libc functions

Quoted 6 lines

    or

b.  change up these functions to better fit their use cases in pc-bios/s390-ccw

Does that make sense?  What do you think?

Keeping them libc-like likely makes sense ... but could we somehow also make sure that we're not running into unexpected errors when using them? Something like "IPL_assert(entries < 1000, ...)" before calling the functions in patch 5?

Thomas

#5 lore raw

Re: [Qemu-devel] [qemu-s390x] [PATCH v2 1/5] s390-ccw: update libc

Quoted 102 lines · Thomas Huth
Quoted 101 lines · Collin L. Walling
Quoted 67 lines · Thomas Huth
Quoted 25 lines · Collin L. Walling

Moved:
   memcmp from bootmap.h to libc.h (renamed from _memcmp) strlen from sclp.c to libc.h (renamed from _strlen)

Added C standard functions:
   isdigit
   atoi

Added non-C standard function:
   itostr

---
   pc-bios/s390-ccw/Makefile  |  2 +-
   pc-bios/s390-ccw/bootmap.c |  4 +--
   pc-bios/s390-ccw/bootmap.h | 16 +---------
   pc-bios/s390-ccw/libc.c    | 75
++++++++++++++++++++++++++++++++++++++++++++++
   pc-bios/s390-ccw/libc.h    | 31 +++++++++++++++++++
   pc-bios/s390-ccw/main.c    | 17 +----------
   pc-bios/s390-ccw/sclp.c    | 10 +------
   7 files changed, 112 insertions(+), 43 deletions(-)

  create mode 100644 pc-bios/s390-ccw/libc.c

[...]

Quoted 20 lines
pc-bios/s390-ccw/libc.c new file +8 -0
+
+/**
+ * itostr:
+ * @num: the integer to be converted.
+ * @str: a pointer to a string to store the conversion.
+ * @len: the length of the passed string.
+ *
+ * Given an integer @num, convert it to a string. The string @str

must be
+ * allocated beforehand. The resulting string will be null terminated and
+ * returned.
+ *
+ * Returns: the string @str of the converted integer @num.
+ */

pc-bios/s390-ccw/libc.c new file +5 -0
+char *itostr(int num, char *str, size_t len)
+{
+    long num_len = 1;
+    int tmp = num;
+    int i;

pc-bios/s390-ccw/libc.c line 64

Quoted 10 lines
pc-bios/s390-ccw/libc.c new file +10 -0
+
+    /* Count length of num */
+    while ((tmp /= 10) > 0) {
+        num_len++;
+    }
+
+    /* Check if we have enough space for num and null */
+    if (len < num_len) {
+        return 0;
+    }

I'm afraid, but I think you've got an off-by-one bug in this code.

In patch 5, you're using this function like this:

     char tmp[4];

     sclp_print(itostr(entries, tmp, sizeof(tmp)));

That means if entries is >= 1000 for example, num_len is 4 ...

Quoted 8 lines
Quoted 7 lines

+    /* Convert int to string */
+    for (i = num_len - 1; i >= 0; i--) {
+        str[i] = num % 10 + '0';
+        num /= 10;
+    }
+
+    str[num_len] = '\0';

... and then you run into a buffer overflow here.

Doh, you're correct.  I forgot to put a "<=" in the len / num_len check. That should fix things up.  Thanks for catching that.

Quoted 4 lines
Quoted 2 lines

+    return str;
+}

Maybe it would also make more sense to panic() instead of "return 0" since you don't check the return value in patch 5 ?

I'm a bit conflicted about doing something like that.  I'm not sure if there's any kind
of guideline we want to follow for defining functions in libc.

I see one of two possibilities:

a.  define these functions as "libc-like" as possible, and use them as if they were
     regular standard libc functions

    or

b.  change up these functions to better fit their use cases in pc-bios/s390-ccw

Quoted 9 lines
Quoted 1 line

Does that make sense?  What do you think?

Keeping them libc-like likely makes sense ... but could we somehow also make sure that we're not running into unexpected errors when using them? Something like "IPL_assert(entries < 1000, ...)" before calling the functions in patch 5?

Thomas

Sounds good to me.

Signature · 1 line
- Collin L Walling
#6 lore raw

Re: [Qemu-devel] [qemu-s390x] [PATCH v2 1/5] s390-ccw: update libc

Quoted 109 lines · Collin L. Walling
Quoted 108 lines · Thomas Huth
Quoted 96 lines · Collin L. Walling
Quoted 67 lines · Thomas Huth
Quoted 25 lines · Collin L. Walling

Moved:
    memcmp from bootmap.h to libc.h (renamed from _memcmp) strlen from sclp.c to libc.h (renamed from _strlen)

Added C standard functions:
    isdigit
    atoi

Added non-C standard function:
    itostr

---
   pc-bios/s390-ccw/Makefile  |  2 +-
   pc-bios/s390-ccw/bootmap.c |  4 +--
   pc-bios/s390-ccw/bootmap.h | 16 +---------
   pc-bios/s390-ccw/libc.c    | 75
++++++++++++++++++++++++++++++++++++++++++++++

   pc-bios/s390-ccw/libc.h    | 31 +++++++++++++++++++
   pc-bios/s390-ccw/main.c    | 17 +----------
   pc-bios/s390-ccw/sclp.c    | 10 +------
   7 files changed, 112 insertions(+), 43 deletions(-)

   create mode 100644 pc-bios/s390-ccw/libc.c

[...]

Quoted 20 lines
pc-bios/s390-ccw/libc.c new file +8 -0
+
+/**
+ * itostr:
+ * @num: the integer to be converted.
+ * @str: a pointer to a string to store the conversion.
+ * @len: the length of the passed string.
+ *
+ * Given an integer @num, convert it to a string. The string @str

must be
+ * allocated beforehand. The resulting string will be null terminated and
+ * returned.
+ *
+ * Returns: the string @str of the converted integer @num.
+ */

pc-bios/s390-ccw/libc.c new file +5 -0
+char *itostr(int num, char *str, size_t len)
+{
+    long num_len = 1;
+    int tmp = num;
+    int i;

pc-bios/s390-ccw/libc.c line 64

Quoted 10 lines
pc-bios/s390-ccw/libc.c new file +10 -0
+
+    /* Count length of num */
+    while ((tmp /= 10) > 0) {
+        num_len++;
+    }
+
+    /* Check if we have enough space for num and null */
+    if (len < num_len) {
+        return 0;
+    }

I'm afraid, but I think you've got an off-by-one bug in this code.

In patch 5, you're using this function like this:

      char tmp[4];

      sclp_print(itostr(entries, tmp, sizeof(tmp)));

That means if entries is >= 1000 for example, num_len is 4 ...

Quoted 8 lines
Quoted 7 lines

+    /* Convert int to string */
+    for (i = num_len - 1; i >= 0; i--) {
+        str[i] = num % 10 + '0';
+        num /= 10;
+    }
+
+    str[num_len] = '\0';

... and then you run into a buffer overflow here.

Doh, you're correct.  I forgot to put a "<=" in the len / num_len check. That should fix things up.  Thanks for catching that.

Quoted 4 lines
Quoted 2 lines

+    return str;
+}

Maybe it would also make more sense to panic() instead of "return 0" since you don't check the return value in patch 5 ?

I'm a bit conflicted about doing something like that.  I'm not sure if there's any kind
of guideline we want to follow for defining functions in libc.

I see one of two possibilities:

a.  define these functions as "libc-like" as possible, and use them as if they were
      regular standard libc functions

Quoted 6 lines

     or

b.  change up these functions to better fit their use cases in pc-bios/s390-ccw

Does that make sense?  What do you think?

Keeping them libc-like likely makes sense ... but could we somehow also make sure that we're not running into unexpected errors when using them? Something like "IPL_assert(entries < 1000, ...)" before calling the functions in patch 5?

Quoted 6 lines
Quoted 3 lines

  Thomas

Sounds good to me.

What if we made a wrapper function for itostr. This func will have a tmp variable that stores the return of itostr. We then do an assertion to make sure we did not return 0 (which indicates that the size of the array was not large enough). If we pass, then just return tmp.

e.g.

static char *_itostr(int num, char *str, size_t len) {
    ...
}

char *itostr(int num, char *str, size_t len)
{
    char *tmp = _itostr(num, str, len);
    IPL_assert(tmp != 0, "array too small for itostr conversion"); return tmp;
}

And as a side note: we know that the number of boot table entries for both ECKD DASD andSCSI cannot exceed 31, so we should be safe with a rather small value for our char arrays.

Signature · 1 line
- Collin L Walling
#7 lore raw

Re: [Qemu-devel] [qemu-s390x] [PATCH v2 1/5] s390-ccw: update libc

Quoted 134 lines · Collin L. Walling
Quoted 110 lines · Collin L. Walling
Quoted 109 lines · Thomas Huth
Quoted 97 lines · Collin L. Walling
Quoted 67 lines · Thomas Huth
Quoted 25 lines · Collin L. Walling

Moved:
    memcmp from bootmap.h to libc.h (renamed from _memcmp) strlen from sclp.c to libc.h (renamed from _strlen)

Added C standard functions:
    isdigit
    atoi

Added non-C standard function:
    itostr

---
   pc-bios/s390-ccw/Makefile  |  2 +-
   pc-bios/s390-ccw/bootmap.c |  4 +--
   pc-bios/s390-ccw/bootmap.h | 16 +---------
   pc-bios/s390-ccw/libc.c    | 75
++++++++++++++++++++++++++++++++++++++++++++++

   pc-bios/s390-ccw/libc.h    | 31 +++++++++++++++++++
   pc-bios/s390-ccw/main.c    | 17 +----------
   pc-bios/s390-ccw/sclp.c    | 10 +------
   7 files changed, 112 insertions(+), 43 deletions(-)

   create mode 100644 pc-bios/s390-ccw/libc.c

[...]

Quoted 20 lines
pc-bios/s390-ccw/libc.c new file +8 -0
+
+/**
+ * itostr:
+ * @num: the integer to be converted.
+ * @str: a pointer to a string to store the conversion.
+ * @len: the length of the passed string.
+ *
+ * Given an integer @num, convert it to a string. The string @str

must be
+ * allocated beforehand. The resulting string will be null terminated and
+ * returned.
+ *
+ * Returns: the string @str of the converted integer @num.
+ */

pc-bios/s390-ccw/libc.c new file +5 -0
+char *itostr(int num, char *str, size_t len)
+{
+    long num_len = 1;
+    int tmp = num;
+    int i;

pc-bios/s390-ccw/libc.c line 64

Quoted 10 lines
pc-bios/s390-ccw/libc.c new file +10 -0
+
+    /* Count length of num */
+    while ((tmp /= 10) > 0) {
+        num_len++;
+    }
+
+    /* Check if we have enough space for num and null */
+    if (len < num_len) {
+        return 0;
+    }

I'm afraid, but I think you've got an off-by-one bug in this code.

In patch 5, you're using this function like this:

      char tmp[4];

      sclp_print(itostr(entries, tmp, sizeof(tmp)));

That means if entries is >= 1000 for example, num_len is 4 ...

Quoted 8 lines
Quoted 7 lines

+    /* Convert int to string */
+    for (i = num_len - 1; i >= 0; i--) {
+        str[i] = num % 10 + '0';
+        num /= 10;
+    }
+
+    str[num_len] = '\0';

... and then you run into a buffer overflow here.

Doh, you're correct.  I forgot to put a "<=" in the len / num_len check.
That should fix things up.  Thanks for catching that.

Quoted 4 lines
Quoted 2 lines

+    return str;
+}

Maybe it would also make more sense to panic() instead of "return 0" since you don't check the return value in patch 5 ?

I'm a bit conflicted about doing something like that.  I'm not sure if there's any kind
of guideline we want to follow for defining functions in libc.

I see one of two possibilities:

a.  define these functions as "libc-like" as possible, and use them as if they were
      regular standard libc functions

Quoted 6 lines

     or

b.  change up these functions to better fit their use cases in pc-bios/s390-ccw

Does that make sense?  What do you think?

Keeping them libc-like likely makes sense ... but could we somehow also make sure that we're not running into unexpected errors when using them? Something like "IPL_assert(entries < 1000, ...)" before calling the functions in patch 5?

Quoted 6 lines
Quoted 3 lines

  Thomas

Sounds good to me.

What if we made a wrapper function for itostr. This func will have a tmp variable
that stores the return of itostr. We then do an assertion to make sure we did not
return 0 (which indicates that the size of the array was not large enough). If we
pass, then just return tmp.

e.g.

static char *_itostr(int num, char *str, size_t len) {
    ...
}

Quoted 6 lines

char *itostr(int num, char *str, size_t len)
{
    char *tmp = _itostr(num, str, len);
    IPL_assert(tmp != 0, "array too small for itostr conversion"); return tmp;
}

That's fine for me, too.

Thomas

#8 lore raw

PATCH v2 2/5 s390-ccw: ipl structs for eckd cdl/ldl

In reply to Collin L. Walling

ECKD DASDs have different IPL structures for CDL and LDL formats. The current Ipl1 and Ipl2 structs follow the CDL format, so we prepend "EckdCdl" to them. Boot info for LDL has been moved to a new struct: EckdLdlIpl1.

Also introduce structs for IPL stages 1 and 1b and for disk geometry.

---
 pc-bios/s390-ccw/bootmap.c | 24 ++++++++++----------
 pc-bios/s390-ccw/bootmap.h | 55 +++++++++++++++++++++++++++++++++-------------
 2 files changed, 53 insertions(+), 26 deletions(-)
pc-bios/s390-ccw/bootmap.c +13 -11
index 6f8e30f..5546b79 100644
@@ -221,9 +221,9 @@ static void run_eckd_boot_script(block_number_t mbr_block_nr)
 static void ipl_eckd_cdl(void)
 {
     XEckdMbr *mbr;
-    Ipl2 *ipl2 = (void *)sec;
+    EckdCdlIpl2 *ipl2 = (void *)sec;
     IplVolumeLabel *vlbl = (void *)sec;
-    block_number_t block_nr;
+    block_number_t mbr_block_nr;
 
     /* we have just read the block #0 and recognized it as "IPL1" */
     sclp_print("CDL\n");
@@ -231,7 +231,7 @@ static void ipl_eckd_cdl(void)
     memset(sec, FREE_SPACE_FILLER, sizeof(sec));
     read_block(1, ipl2, "Cannot read IPL2 record at block 1");
 
-    mbr = &ipl2->u.x.mbr;
+    mbr = &ipl2->mbr;
     IPL_assert(magic_match(mbr, ZIPL_MAGIC), "No zIPL section in IPL2 record.");
     IPL_assert(block_size_ok(mbr->blockptr.xeckd.bptr.size),
                "Bad block size in zIPL section of IPL2 record.");
@@ -239,7 +239,7 @@ static void ipl_eckd_cdl(void)
                "Non-ECKD device type in zIPL section of IPL2 record.");
 
     /* save pointer to Boot Script */
-    block_nr = eckd_block_num((void *)&(mbr->blockptr));
+    mbr_block_nr = eckd_block_num((void *)&(mbr->blockptr));
 
     memset(sec, FREE_SPACE_FILLER, sizeof(sec));
     read_block(2, vlbl, "Cannot read Volume Label at block 2");
@@ -249,7 +249,7 @@ static void ipl_eckd_cdl(void)
                "Invalid magic of volser block");
     print_volser(vlbl->f.volser);
 
-    run_eckd_boot_script(block_nr);
+    run_eckd_boot_script(mbr_block_nr);
     /* no return */
 }
 
@@ -280,8 +280,8 @@ static void print_eckd_ldl_msg(ECKD_IPL_mode_t mode)
 
 static void ipl_eckd_ldl(ECKD_IPL_mode_t mode)
 {
-    block_number_t block_nr;
-    BootInfo *bip = (void *)(sec + 0x70); /* BootInfo is MBR for LDL */
+    block_number_t mbr_block_nr;
+    EckdLdlIpl1 *ipl1 = (void *)sec;
 
     if (mode != ECKD_LDL_UNLABELED) {
         print_eckd_ldl_msg(mode);
@@ -292,15 +292,17 @@ static void ipl_eckd_ldl(ECKD_IPL_mode_t mode)
     memset(sec, FREE_SPACE_FILLER, sizeof(sec));
     read_block(0, sec, "Cannot read block 0 to grab boot info.");
     if (mode == ECKD_LDL_UNLABELED) {
-        if (!magic_match(bip->magic, ZIPL_MAGIC)) {
+        if (!magic_match(ipl1->boot_info.magic, ZIPL_MAGIC)) {
             return; /* not applicable layout */
         }
         sclp_print("unlabeled LDL.\n");
     }
-    verify_boot_info(bip);
+    verify_boot_info(&ipl1->boot_info);
+
+    mbr_block_nr =
+        eckd_block_num((void *)&(ipl1->boot_info.bp.ipl.bm_ptr.eckd.bptr));
 
-    block_nr = eckd_block_num((void *)&(bip->bp.ipl.bm_ptr.eckd.bptr));
-    run_eckd_boot_script(block_nr);
+    run_eckd_boot_script(mbr_block_nr);
     /* no return */
 }
 
pc-bios/s390-ccw/bootmap.h +40 -15

62 lines not on this page · raw message

Signature · 1 line
2.7.4
#9 lore raw

Re: [Qemu-devel] [PATCH v2 2/5] s390-ccw: ipl structs for eckd cdl/ldl

Quoted 16 lines · On Mon, 11 Dec 2017 17:19:17 -0500 "Collin L. Walling"…

ECKD DASDs have different IPL structures for CDL and LDL formats. The current Ipl1 and Ipl2 structs follow the CDL format, so we prepend "EckdCdl" to them. Boot info for LDL has been moved to a new struct: EckdLdlIpl1.

Also introduce structs for IPL stages 1 and 1b and for disk geometry.

---
 pc-bios/s390-ccw/bootmap.c | 24 ++++++++++----------
 pc-bios/s390-ccw/bootmap.h | 55 +++++++++++++++++++++++++++++++++-------------
 2 files changed, 53 insertions(+), 26 deletions(-)

pc-bios/s390-ccw/bootmap.c static void ipl_eckd_ldl(ECKD_IPL_mode_t mode) · line 303

Quoted 2 lines
pc-bios/s390-ccw/bootmap.c +2 -0
+    mbr_block_nr =
+        eckd_block_num((void *)&(ipl1->boot_info.bp.ipl.bm_ptr.eckd.bptr));

Let me nominate this as "crazy nested struct of the week".

(Just kidding, your patch certainly improves things in general :)

pc-bios/s390-ccw/bootmap.h typedef struct BootInfo { /* @ 0x70, record #0 */ · line 245

Quoted 7 lines
pc-bios/s390-ccw/bootmap.h +7 -0
+typedef struct EckdSeekarg {
+    uint16_t pad;
+    uint16_t cyl;
+    uint16_t head;
+    uint8_t sec;
+    uint8_t pad2;
+} __attribute__ ((packed)) EckdSeekarg;

Maybe make this EckdSeekArg?

#10 lore raw

Re: [Qemu-devel] [qemu-s390x] [PATCH v2 2/5] s390-ccw: ipl structs for eckd cdl/ldl

Quoted 18 lines · Cornelia Huck
Quoted 15 lines · On Mon, 11 Dec 2017 17:19:17 -0500 "Collin L. Walling"…

ECKD DASDs have different IPL structures for CDL and LDL formats. The current Ipl1 and Ipl2 structs follow the CDL format, so we prepend "EckdCdl" to them. Boot info for LDL has been moved to a new struct: EckdLdlIpl1.

Also introduce structs for IPL stages 1 and 1b and for disk geometry.

---
  pc-bios/s390-ccw/bootmap.c | 24 ++++++++++----------
  pc-bios/s390-ccw/bootmap.h | 55 +++++++++++++++++++++++++++++++++-------------
  2 files changed, 53 insertions(+), 26 deletions(-)
Quoted 5 lines

pc-bios/s390-ccw/bootmap.c static void ipl_eckd_ldl(ECKD_IPL_mode_t mode) · line 303

Quoted 2 lines
pc-bios/s390-ccw/bootmap.c +2 -0
+    mbr_block_nr =
+        eckd_block_num((void *)&(ipl1->boot_info.bp.ipl.bm_ptr.eckd.bptr));

Let me nominate this as "crazy nested struct of the week".

(Just kidding, your patch certainly improves things in general :)

Haha, well I am honored! (Cannot take full credit -- it was quite crazy before :)

Quoted 11 lines

pc-bios/s390-ccw/bootmap.h typedef struct BootInfo { /* @ 0x70, record #0 */ · line 245

Quoted 9 lines
pc-bios/s390-ccw/bootmap.h +7 -0
+typedef struct EckdSeekarg {
+    uint16_t pad;
+    uint16_t cyl;
+    uint16_t head;
+    uint8_t sec;
+    uint8_t pad2;
+} __attribute__ ((packed)) EckdSeekarg;

Maybe make this EckdSeekArg?

I'm pretty sure "Seekarg" is not a word, so your suggestion makes sense. (plus it reads better imo)

Signature · 1 line
- Collin L Walling
#11 lore raw

Re: [Qemu-devel] [qemu-s390x] [PATCH v2 2/5] s390-ccw: ipl structs for eckd cdl/ldl

In reply to Cornelia Huck
Quoted 18 lines · Cornelia Huck
Quoted 15 lines · On Mon, 11 Dec 2017 17:19:17 -0500 "Collin L. Walling"…

ECKD DASDs have different IPL structures for CDL and LDL formats. The current Ipl1 and Ipl2 structs follow the CDL format, so we prepend "EckdCdl" to them. Boot info for LDL has been moved to a new struct: EckdLdlIpl1.

Also introduce structs for IPL stages 1 and 1b and for disk geometry.

---
  pc-bios/s390-ccw/bootmap.c | 24 ++++++++++----------
  pc-bios/s390-ccw/bootmap.h | 55 +++++++++++++++++++++++++++++++++-------------
  2 files changed, 53 insertions(+), 26 deletions(-)
Quoted 5 lines

pc-bios/s390-ccw/bootmap.c static void ipl_eckd_ldl(ECKD_IPL_mode_t mode) · line 303

Quoted 2 lines
pc-bios/s390-ccw/bootmap.c +2 -0
+    mbr_block_nr =
+        eckd_block_num((void *)&(ipl1->boot_info.bp.ipl.bm_ptr.eckd.bptr));

Let me nominate this as "crazy nested struct of the week".

(Just kidding, your patch certainly improves things in general :)

FWIW: we can reduce it to just ipl1->boot_info.bp-- the way the structs are unioned and the ordering of the fields make this possible.  Thoughts?

Quoted 11 lines

pc-bios/s390-ccw/bootmap.h typedef struct BootInfo { /* @ 0x70, record #0 */ · line 245

Quoted 9 lines
pc-bios/s390-ccw/bootmap.h +7 -0
+typedef struct EckdSeekarg {
+    uint16_t pad;
+    uint16_t cyl;
+    uint16_t head;
+    uint8_t sec;
+    uint8_t pad2;
+} __attribute__ ((packed)) EckdSeekarg;

Maybe make this EckdSeekArg?

Signature · 1 line
- Collin L Walling
#12 lore raw

Re: [Qemu-devel] [qemu-s390x] [PATCH v2 2/5] s390-ccw: ipl structs for eckd cdl/ldl

Quoted 21 lines · On Mon, 18 Dec 2017 17:11:52 -0500 "Collin L. Walling"…
Quoted 20 lines · Cornelia Huck
Quoted 17 lines · On Mon, 11 Dec 2017 17:19:17 -0500 "Collin L. Walling"…

ECKD DASDs have different IPL structures for CDL and LDL formats. The current Ipl1 and Ipl2 structs follow the CDL format, so we prepend "EckdCdl" to them. Boot info for LDL has been moved to a new struct: EckdLdlIpl1.

Also introduce structs for IPL stages 1 and 1b and for disk geometry.

---
  pc-bios/s390-ccw/bootmap.c | 24 ++++++++++----------
  pc-bios/s390-ccw/bootmap.h | 55 +++++++++++++++++++++++++++++++++-------------
  2 files changed, 53 insertions(+), 26 deletions(-)
pc-bios/s390-ccw/bootmap.c +2 -0
+    mbr_block_nr =
+        eckd_block_num((void *)&(ipl1->boot_info.bp.ipl.bm_ptr.eckd.bptr));  
Quoted 8 lines
Quoted 3 lines

Let me nominate this as "crazy nested struct of the week".

(Just kidding, your patch certainly improves things in general :)

FWIW: we can reduce it to just ipl1->boot_info.bp-- the way the structs are unioned
and the ordering of the fields make this possible.  Thoughts?

Ah, missed that one. I'd prefer to be explicit here, even if it is long.

#13 lore raw

PATCH v2 3/5 s390-ccw: parse and set boot menu options

In reply to Collin L. Walling

Set boot menu options for an s390 guest and store them in the iplb. These options are set via the QEMU command line option:

-boot menu=on|off[,splash-time=X]

or via the libvirt domain xml:

    <os>
      <bootmenu enable='yes|no' timeout='X'/>
    </os>

Where X represents some positive integer representing milliseconds.

A loadparm other than 'prompt' will disable the menu and just boot the specified entry.

---
 hw/s390x/ipl.c          | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
 hw/s390x/ipl.h          |  8 +++++--
 pc-bios/s390-ccw/iplb.h |  8 +++++--
 3 files changed, 67 insertions(+), 4 deletions(-)
hw/s390x/ipl.c +55 -0
index 0d06fc1..ed5e8d1 100644
@@ -23,6 +23,8 @@
 #include "hw/s390x/ebcdic.h"
 #include "ipl.h"
 #include "qemu/error-report.h"
+#include "qemu/config-file.h"
+#include "qemu/cutils.h"
 
 #define KERN_IMAGE_START                0x010000UL
 #define KERN_PARM_AREA                  0x010480UL
@@ -33,6 +35,9 @@
 #define ZIPL_IMAGE_START                0x009000UL
 #define IPL_PSW_MASK                    (PSW_MASK_32 | PSW_MASK_64)
 
+#define BOOT_MENU_FLAG_BOOT_OPTS 0x80
+#define BOOT_MENU_FLAG_ZIPL_OPTS 0x40
+
 static bool iplb_extended_needed(void *opaque)
 {
     S390IPLState *ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
@@ -219,6 +224,51 @@ static Property s390_ipl_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+static void s390_ipl_set_boot_menu(uint8_t *boot_menu_flags,
+                                   uint16_t *boot_menu_timeout)
+{
+    MachineState *machine = MACHINE(qdev_get_machine());
+    char *lp = object_property_get_str(OBJECT(machine), "loadparm", NULL);
+    QemuOptsList *plist = qemu_find_opts("boot-opts");
+    QemuOpts *opts = QTAILQ_FIRST(&plist->head);
+    const char *p = qemu_opt_get(opts, "menu");
+    unsigned long timeout = 0;
+
+    if (memcmp(lp, "PROMPT  ", 8) == 0) {
+        *boot_menu_flags = BOOT_MENU_FLAG_BOOT_OPTS;
+
+    } else if (*lp) {
+        /* If loadparm is set to any value, then discard boot menu */
+        return;
+
+    } else if (!p) {
+        /* In the absence of -boot menu, use zipl loader parameters */
+        *boot_menu_flags = BOOT_MENU_FLAG_ZIPL_OPTS;
+
+    } else if (strncmp(p, "on", 2) == 0) {
+        *boot_menu_flags = BOOT_MENU_FLAG_BOOT_OPTS;
+
+        p = qemu_opt_get(opts, "splash-time");
+
+        if (p && qemu_strtoul(p, NULL, 10, &timeout)) {
+            error_report("splash-time value is invalid, forcing it to 0.");
+            return;
+        }
+
+        /* Store timeout value as seconds */
+        timeout /= 1000;
+
+        if (timeout > 0xffff) {
+            error_report("splash-time value is greater than 65535000,"
+                         " forcing it to 65535000.");
+            *boot_menu_timeout = 0xffff;
+            return;
+        }
+
+        *boot_menu_timeout = timeout;
+    }
+}
+
 static bool s390_gen_initial_iplb(S390IPLState *ipl)
 {
     DeviceState *dev_st;
@@ -245,6 +295,8 @@ static bool s390_gen_initial_iplb(S390IPLState *ipl)
             ipl->iplb.pbt = S390_IPL_TYPE_CCW;
             ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
             ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3;
+            s390_ipl_set_boot_menu(&ipl->iplb.ccw.boot_menu_flags,
+                                   &ipl->iplb.ccw.boot_menu_timeout);
         } else if (sd) {
             SCSIBus *bus = scsi_bus_from_device(sd);
             VirtIOSCSI *vdev = container_of(bus, VirtIOSCSI, bus);
@@ -266,6 +318,8 @@ static bool s390_gen_initial_iplb(S390IPLState *ipl)
             ipl->iplb.scsi.channel = cpu_to_be16(sd->channel);
             ipl->iplb.scsi.devno = cpu_to_be16(ccw_dev->sch->devno);
             ipl->iplb.scsi.ssid = ccw_dev->sch->ssid & 3;
+            s390_ipl_set_boot_menu(&ipl->iplb.scsi.boot_menu_flags,
+                                   &ipl->iplb.scsi.boot_menu_timeout);
         } else {
             return false; /* unknown device */
         }
@@ -273,6 +327,7 @@ static bool s390_gen_initial_iplb(S390IPLState *ipl)
         if (!s390_ipl_set_loadparm(ipl->iplb.loadparm)) {
             ipl->iplb.flags |= DIAG308_FLAGS_LP_VALID;
         }
+
         return true;
     }
 
hw/s390x/ipl.h +6 -2
index 8a705e0..ff3b397 100644
@@ -17,7 +17,9 @@
 
 struct IplBlockCcw {
     uint64_t netboot_start_addr;
-    uint8_t  reserved0[77];
+    uint8_t  reserved0[74];
+    uint16_t boot_menu_timeout;
+    uint8_t  boot_menu_flags;
     uint8_t  ssid;
     uint16_t devno;
     uint8_t  vm_flags;
@@ -51,7 +53,9 @@ struct IplBlockQemuScsi {
     uint32_t lun;
     uint16_t target;
     uint16_t channel;
-    uint8_t  reserved0[77];
+    uint8_t  reserved0[74];
+    uint16_t boot_menu_timeout;
+    uint8_t  boot_menu_flags;
     uint8_t  ssid;
     uint16_t devno;
 } QEMU_PACKED;
pc-bios/s390-ccw/iplb.h +6 -2
index 890aed9..fe909d2 100644
@@ -14,7 +14,9 @@
 
 struct IplBlockCcw {
     uint64_t netboot_start_addr;
-    uint8_t  reserved0[77];
+    uint8_t  reserved0[74];
+    uint16_t boot_menu_timeout;
+    uint8_t  boot_menu_flags;
     uint8_t  ssid;
     uint16_t devno;
     uint8_t  vm_flags;
@@ -48,7 +50,9 @@ struct IplBlockQemuScsi {
     uint32_t lun;
     uint16_t target;
     uint16_t channel;
-    uint8_t  reserved0[77];
+    uint8_t  reserved0[74];
+    uint16_t boot_menu_timeout;
+    uint8_t  boot_menu_flags;
     uint8_t  ssid;
     uint16_t devno;
 } __attribute__ ((packed));
Signature · 1 line
2.7.4
#14 lore raw

Re: [Qemu-devel] [PATCH v2 3/5] s390-ccw: parse and set boot menu options

Quoted 40 lines · Collin L. Walling

Set boot menu options for an s390 guest and store them in the iplb. These options are set via the QEMU command line option:

-boot menu=on|off[,splash-time=X]

or via the libvirt domain xml:

    <os>
      <bootmenu enable='yes|no' timeout='X'/>
    </os>

Where X represents some positive integer representing milliseconds.

A loadparm other than 'prompt' will disable the menu and just boot the specified entry.

---
 hw/s390x/ipl.c          | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
 hw/s390x/ipl.h          |  8 +++++--
 pc-bios/s390-ccw/iplb.h |  8 +++++--
 3 files changed, 67 insertions(+), 4 deletions(-)
hw/s390x/ipl.c +2 -0
index 0d06fc1..ed5e8d1 100644
@@ -23,6 +23,8 @@
 #include "hw/s390x/ebcdic.h"
 #include "ipl.h"
 #include "qemu/error-report.h"
+#include "qemu/config-file.h"
+#include "qemu/cutils.h"
 
 #define KERN_IMAGE_START                0x010000UL
 #define KERN_PARM_AREA                  0x010480UL

hw/s390x/ipl.c line 39

Quoted 5 lines
hw/s390x/ipl.c +2 -0
@@ -33,6 +35,9 @@
 #define ZIPL_IMAGE_START                0x009000UL
 #define IPL_PSW_MASK                    (PSW_MASK_32 | PSW_MASK_64)
 
+#define BOOT_MENU_FLAG_BOOT_OPTS 0x80
+#define BOOT_MENU_FLAG_ZIPL_OPTS 0x40

these should go to ipl.h and maybe be renamed to something like

IPL_BLOCK_... (not sure if even CCW/SCSI specific ones make sense)

Quoted 11 lines
hw/s390x/ipl.c +4 -0
+
 static bool iplb_extended_needed(void *opaque)
 {
     S390IPLState *ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
@@ -219,6 +224,51 @@ static Property s390_ipl_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+static void s390_ipl_set_boot_menu(uint8_t *boot_menu_flags,
+                                   uint16_t *boot_menu_timeout)
+{

hw/s390x/ipl.c static Property s390_ipl_properties[] = { · line 239

Quoted 10 lines
hw/s390x/ipl.c +10 -0
+    MachineState *machine = MACHINE(qdev_get_machine());
+    char *lp = object_property_get_str(OBJECT(machine), "loadparm", NULL);
+    QemuOptsList *plist = qemu_find_opts("boot-opts");
+    QemuOpts *opts = QTAILQ_FIRST(&plist->head);
+    const char *p = qemu_opt_get(opts, "menu");
+    unsigned long timeout = 0;
+
+    if (memcmp(lp, "PROMPT  ", 8) == 0) {
+        *boot_menu_flags = BOOT_MENU_FLAG_BOOT_OPTS;
+

remove this empty line

hw/s390x/ipl.c static Property s390_ipl_properties[] = { · line 241

Quoted 2 lines
hw/s390x/ipl.c +2 -0
+    } else if (*lp) {
+        /* If loadparm is set to any value, then discard boot menu */

I find the connection between -boot and loadparm quite confusing. Shouldn't the s390-ccw bios handle this? You should just forward here what is given via -boot IMHO.

Quoted 8 lines
hw/s390x/ipl.c +8 -0
+        return;
+
+    } else if (!p) {
+        /* In the absence of -boot menu, use zipl loader parameters */
+        *boot_menu_flags = BOOT_MENU_FLAG_ZIPL_OPTS;
+
+    } else if (strncmp(p, "on", 2) == 0) {
+        *boot_menu_flags = BOOT_MENU_FLAG_BOOT_OPTS;

hw/s390x/ipl.c static Property s390_ipl_properties[] = { · line 259

Quoted 10 lines
hw/s390x/ipl.c +10 -0
+
+        p = qemu_opt_get(opts, "splash-time");
+
+        if (p && qemu_strtoul(p, NULL, 10, &timeout)) {
+            error_report("splash-time value is invalid, forcing it to 0.");
+            return;
+        }
+
+        /* Store timeout value as seconds */
+        timeout /= 1000;

So we pass ms on the command line? Why?

Quoted 25 lines
hw/s390x/ipl.c +14 -0
+
+        if (timeout > 0xffff) {
+            error_report("splash-time value is greater than 65535000,"
+                         " forcing it to 65535000.");
+            *boot_menu_timeout = 0xffff;
+            return;
+        }
+
+        *boot_menu_timeout = timeout;
+    }
+}
+
 static bool s390_gen_initial_iplb(S390IPLState *ipl)
 {
     DeviceState *dev_st;
@@ -245,6 +295,8 @@ static bool s390_gen_initial_iplb(S390IPLState *ipl)
             ipl->iplb.pbt = S390_IPL_TYPE_CCW;
             ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
             ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3;
+            s390_ipl_set_boot_menu(&ipl->iplb.ccw.boot_menu_flags,
+                                   &ipl->iplb.ccw.boot_menu_timeout);
         } else if (sd) {
             SCSIBus *bus = scsi_bus_from_device(sd);
             VirtIOSCSI *vdev = container_of(bus, VirtIOSCSI, bus);

hw/s390x/ipl.c static bool s390_gen_initial_iplb(S390IPLState *ipl) · line 322

Quoted 5 lines
hw/s390x/ipl.c +2 -0
@@ -266,6 +318,8 @@ static bool s390_gen_initial_iplb(S390IPLState *ipl)
             ipl->iplb.scsi.channel = cpu_to_be16(sd->channel);
             ipl->iplb.scsi.devno = cpu_to_be16(ccw_dev->sch->devno);
             ipl->iplb.scsi.ssid = ccw_dev->sch->ssid & 3;
+            s390_ipl_set_boot_menu(&ipl->iplb.scsi.boot_menu_flags,
+                                   &ipl->iplb.scsi.boot_menu_timeout);

can you call this only once for both cases below and only pass ipl? The function can figure out using ipl->iplb.pbt what to set.

hw/s390x/ipl.c static bool s390_gen_initial_iplb(S390IPLState *ipl) · line 330

Quoted 8 lines
hw/s390x/ipl.c +1 -0
         } else {
             return false; /* unknown device */
         }
@@ -273,6 +327,7 @@ static bool s390_gen_initial_iplb(S390IPLState *ipl)
         if (!s390_ipl_set_loadparm(ipl->iplb.loadparm)) {
             ipl->iplb.flags |= DIAG308_FLAGS_LP_VALID;
         }
+

unrelated change

Quoted 46 lines
hw/s390x/ipl.c +0 -0
         return true;
     }
 
hw/s390x/ipl.h +6 -2
index 8a705e0..ff3b397 100644
@@ -17,7 +17,9 @@
 
 struct IplBlockCcw {
     uint64_t netboot_start_addr;
-    uint8_t  reserved0[77];
+    uint8_t  reserved0[74];
+    uint16_t boot_menu_timeout;
+    uint8_t  boot_menu_flags;
     uint8_t  ssid;
     uint16_t devno;
     uint8_t  vm_flags;
@@ -51,7 +53,9 @@ struct IplBlockQemuScsi {
     uint32_t lun;
     uint16_t target;
     uint16_t channel;
-    uint8_t  reserved0[77];
+    uint8_t  reserved0[74];
+    uint16_t boot_menu_timeout;
+    uint8_t  boot_menu_flags;
     uint8_t  ssid;
     uint16_t devno;
 } QEMU_PACKED;
pc-bios/s390-ccw/iplb.h +3 -1
index 890aed9..fe909d2 100644
@@ -14,7 +14,9 @@
 
 struct IplBlockCcw {
     uint64_t netboot_start_addr;
-    uint8_t  reserved0[77];
+    uint8_t  reserved0[74];
+    uint16_t boot_menu_timeout;
+    uint8_t  boot_menu_flags;
     uint8_t  ssid;
     uint16_t devno;
     uint8_t  vm_flags;

pc-bios/s390-ccw/iplb.h struct IplBlockQemuScsi { · line 58

Quoted 10 lines
pc-bios/s390-ccw/iplb.h +3 -1
@@ -48,7 +50,9 @@ struct IplBlockQemuScsi {
     uint32_t lun;
     uint16_t target;
     uint16_t channel;
-    uint8_t  reserved0[77];
+    uint8_t  reserved0[74];
+    uint16_t boot_menu_timeout;
+    uint8_t  boot_menu_flags;
     uint8_t  ssid;
     uint16_t devno;
 } __attribute__ ((packed));

These look good to me (but as discussed, I think HW folks have to agree on this).

Signature · 3 lines
Thanks,

David / dhildenb
#15 lore raw

Re: [Qemu-devel] [qemu-s390x] [PATCH v2 3/5] s390-ccw: parse and set boot menu options

Quoted 44 lines · David Hildenbrand
Quoted 43 lines · Collin L. Walling

Set boot menu options for an s390 guest and store them in the iplb. These options are set via the QEMU command line option:

-boot menu=on|off[,splash-time=X]

or via the libvirt domain xml:

     <os>
       <bootmenu enable='yes|no' timeout='X'/>
     </os>

Where X represents some positive integer representing milliseconds.

A loadparm other than 'prompt' will disable the menu and just boot the specified entry.

---
  hw/s390x/ipl.c          | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
  hw/s390x/ipl.h          |  8 +++++--
  pc-bios/s390-ccw/iplb.h |  8 +++++--
  3 files changed, 67 insertions(+), 4 deletions(-)
hw/s390x/ipl.c +2 -0
index 0d06fc1..ed5e8d1 100644
@@ -23,6 +23,8 @@
  #include "hw/s390x/ebcdic.h"
  #include "ipl.h"
  #include "qemu/error-report.h"
+#include "qemu/config-file.h"
+#include "qemu/cutils.h"
 
  #define KERN_IMAGE_START                0x010000UL
  #define KERN_PARM_AREA                  0x010480UL
@@ -33,6 +35,9 @@
  #define ZIPL_IMAGE_START                0x009000UL
  #define IPL_PSW_MASK                    (PSW_MASK_32 | PSW_MASK_64)
 
Quoted 5 lines

hw/s390x/ipl.c line 39

Quoted 2 lines
hw/s390x/ipl.c +2 -0
+#define BOOT_MENU_FLAG_BOOT_OPTS 0x80
+#define BOOT_MENU_FLAG_ZIPL_OPTS 0x40

these should go to ipl.h and maybe be renamed to something like

IPL_BLOCK_... (not sure if even CCW/SCSI specific ones make sense)

I agree to move them to the header file.

I do not think we need CCW/SCSI specific ones at this time.

I'd prefer to keep the "BOOT_MENU_FLAG*" scheme, as it mirrors the same naming convention Iuse in the bios.

Quoted 24 lines
Quoted 12 lines
hw/s390x/ipl.c +4 -0
+
  static bool iplb_extended_needed(void *opaque)
  {
      S390IPLState *ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
@@ -219,6 +224,51 @@ static Property s390_ipl_properties[] = {
      DEFINE_PROP_END_OF_LIST(),
  };
 
+static void s390_ipl_set_boot_menu(uint8_t *boot_menu_flags,
+                                   uint16_t *boot_menu_timeout)
+{

hw/s390x/ipl.c static Property s390_ipl_properties[] = { · line 239

Quoted 10 lines
hw/s390x/ipl.c +10 -0
+    MachineState *machine = MACHINE(qdev_get_machine());
+    char *lp = object_property_get_str(OBJECT(machine), "loadparm", NULL);
+    QemuOptsList *plist = qemu_find_opts("boot-opts");
+    QemuOpts *opts = QTAILQ_FIRST(&plist->head);
+    const char *p = qemu_opt_get(opts, "menu");
+    unsigned long timeout = 0;
+
+    if (memcmp(lp, "PROMPT  ", 8) == 0) {
+        *boot_menu_flags = BOOT_MENU_FLAG_BOOT_OPTS;
+

remove this empty line

Quoted 5 lines

hw/s390x/ipl.c static Property s390_ipl_properties[] = { · line 241

Quoted 2 lines
hw/s390x/ipl.c +2 -0
+    } else if (*lp) {
+        /* If loadparm is set to any value, then discard boot menu */

I find the connection between -boot and loadparm quite confusing. Shouldn't the s390-ccw bios handle this? You should just forward here what is given via -boot IMHO.

My idea was to handle the setting of all boot flags and timeout options under one function.  IMO, it reads easier when the boot menu options are handled in one place instead of having them potentially changing in the bios (interpreting loadparm).

I'm more-or-less curious to see what the feedback on this will be.  If the consensus is to interpret loadparm in the bios instead, then I'm okay with that too.

Quoted 12 lines
Quoted 12 lines
hw/s390x/ipl.c +11 -0
+        return;
+
+    } else if (!p) {
+        /* In the absence of -boot menu, use zipl loader parameters */
+        *boot_menu_flags = BOOT_MENU_FLAG_ZIPL_OPTS;
+
+    } else if (strncmp(p, "on", 2) == 0) {
+        *boot_menu_flags = BOOT_MENU_FLAG_BOOT_OPTS;
+
+        p = qemu_opt_get(opts, "splash-time");
+
Quoted 8 lines

hw/s390x/ipl.c static Property s390_ipl_properties[] = { · line 259

Quoted 7 lines
hw/s390x/ipl.c +7 -0
+        if (p && qemu_strtoul(p, NULL, 10, &timeout)) {
+            error_report("splash-time value is invalid, forcing it to 0.");
+            return;
+        }
+
+        /* Store timeout value as seconds */
+        timeout /= 1000;

So we pass ms on the command line? Why?

The QEMU user documentation specifies the splash-time as milliseconds. I figured it would be best to conform to that.

Quoted 25 lines
Quoted 25 lines
hw/s390x/ipl.c +14 -0
+
+        if (timeout > 0xffff) {
+            error_report("splash-time value is greater than 65535000,"
+                         " forcing it to 65535000.");
+            *boot_menu_timeout = 0xffff;
+            return;
+        }
+
+        *boot_menu_timeout = timeout;
+    }
+}
+
  static bool s390_gen_initial_iplb(S390IPLState *ipl)
  {
      DeviceState *dev_st;
@@ -245,6 +295,8 @@ static bool s390_gen_initial_iplb(S390IPLState *ipl)
              ipl->iplb.pbt = S390_IPL_TYPE_CCW;
              ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
              ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3;
+            s390_ipl_set_boot_menu(&ipl->iplb.ccw.boot_menu_flags,
+                                   &ipl->iplb.ccw.boot_menu_timeout);
          } else if (sd) {
              SCSIBus *bus = scsi_bus_from_device(sd);
              VirtIOSCSI *vdev = container_of(bus, VirtIOSCSI, bus);
Quoted 8 lines

hw/s390x/ipl.c static bool s390_gen_initial_iplb(S390IPLState *ipl) · line 322

Quoted 6 lines
hw/s390x/ipl.c +2 -0
@@ -266,6 +318,8 @@ static bool s390_gen_initial_iplb(S390IPLState *ipl)
              ipl->iplb.scsi.channel = cpu_to_be16(sd->channel);
              ipl->iplb.scsi.devno = cpu_to_be16(ccw_dev->sch->devno);
              ipl->iplb.scsi.ssid = ccw_dev->sch->ssid & 3;
+            s390_ipl_set_boot_menu(&ipl->iplb.scsi.boot_menu_flags,
+                                   &ipl->iplb.scsi.boot_menu_timeout);

can you call this only once for both cases below and only pass ipl? The function can figure out using ipl->iplb.pbt what to set.

Can do.

Quoted 61 lines

hw/s390x/ipl.c static bool s390_gen_initial_iplb(S390IPLState *ipl) · line 330

Quoted 9 lines
hw/s390x/ipl.c +1 -0
          } else {
              return false; /* unknown device */
          }
@@ -273,6 +327,7 @@ static bool s390_gen_initial_iplb(S390IPLState *ipl)
          if (!s390_ipl_set_loadparm(ipl->iplb.loadparm)) {
              ipl->iplb.flags |= DIAG308_FLAGS_LP_VALID;
          }
+

unrelated change

Quoted 50 lines
hw/s390x/ipl.c +0 -0
          return true;
      }
 
hw/s390x/ipl.h +6 -2
index 8a705e0..ff3b397 100644
@@ -17,7 +17,9 @@
 
  struct IplBlockCcw {
      uint64_t netboot_start_addr;
-    uint8_t  reserved0[77];
+    uint8_t  reserved0[74];
+    uint16_t boot_menu_timeout;
+    uint8_t  boot_menu_flags;
      uint8_t  ssid;
      uint16_t devno;
      uint8_t  vm_flags;
@@ -51,7 +53,9 @@ struct IplBlockQemuScsi {
      uint32_t lun;
      uint16_t target;
      uint16_t channel;
-    uint8_t  reserved0[77];
+    uint8_t  reserved0[74];
+    uint16_t boot_menu_timeout;
+    uint8_t  boot_menu_flags;
      uint8_t  ssid;
      uint16_t devno;
  } QEMU_PACKED;
pc-bios/s390-ccw/iplb.h +4 -2
index 890aed9..fe909d2 100644
@@ -14,7 +14,9 @@
 
  struct IplBlockCcw {
      uint64_t netboot_start_addr;
-    uint8_t  reserved0[77];
+    uint8_t  reserved0[74];
+    uint16_t boot_menu_timeout;
+    uint8_t  boot_menu_flags;
      uint8_t  ssid;
      uint16_t devno;
      uint8_t  vm_flags;
@@ -48,7 +50,9 @@ struct IplBlockQemuScsi {
      uint32_t lun;
      uint16_t target;
      uint16_t channel;
-    uint8_t  reserved0[77];
+    uint8_t  reserved0[74];
Quoted 9 lines

pc-bios/s390-ccw/iplb.h struct IplBlockQemuScsi { · line 58

Quoted 6 lines
pc-bios/s390-ccw/iplb.h +2 -0
+    uint16_t boot_menu_timeout;
+    uint8_t  boot_menu_flags;
      uint8_t  ssid;
      uint16_t devno;
  } __attribute__ ((packed));

These look good to me (but as discussed, I think HW folks have to agree on this).

Thanks for your feedback.

Signature · 1 line
- Collin L Walling
#16 lore raw

Re: [Qemu-devel] [qemu-s390x] [PATCH v2 3/5] s390-ccw: parse and set boot menu options

Quoted 9 lines
Quoted 5 lines

hw/s390x/ipl.c static Property s390_ipl_properties[] = { · line 259

Quoted 4 lines
hw/s390x/ipl.c +3 -0
+
+        /* Store timeout value as seconds */
+        timeout /= 1000;

So we pass ms on the command line? Why?

The QEMU user documentation specifies the splash-time as milliseconds. I figured it would be best to conform to that.

Makes sense then, maybe round up.

Signature · 3 lines
Thanks,

David / dhildenb
#17 lore raw

Re: [Qemu-devel] [PATCH v2 3/5] s390-ccw: parse and set boot menu options

In reply to Collin L. Walling
Quoted 85 lines · Collin L. Walling

Set boot menu options for an s390 guest and store them in the iplb. These options are set via the QEMU command line option:

-boot menu=on|off[,splash-time=X]

or via the libvirt domain xml:

    <os>
      <bootmenu enable='yes|no' timeout='X'/>
    </os>

Where X represents some positive integer representing milliseconds.

A loadparm other than 'prompt' will disable the menu and just boot the specified entry.

---
 hw/s390x/ipl.c          | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
 hw/s390x/ipl.h          |  8 +++++--
 pc-bios/s390-ccw/iplb.h |  8 +++++--
 3 files changed, 67 insertions(+), 4 deletions(-)
hw/s390x/ipl.c +37 -0
index 0d06fc1..ed5e8d1 100644
@@ -23,6 +23,8 @@
 #include "hw/s390x/ebcdic.h"
 #include "ipl.h"
 #include "qemu/error-report.h"
+#include "qemu/config-file.h"
+#include "qemu/cutils.h"
 
 #define KERN_IMAGE_START                0x010000UL
 #define KERN_PARM_AREA                  0x010480UL
@@ -33,6 +35,9 @@
 #define ZIPL_IMAGE_START                0x009000UL
 #define IPL_PSW_MASK                    (PSW_MASK_32 | PSW_MASK_64)
 
+#define BOOT_MENU_FLAG_BOOT_OPTS 0x80
+#define BOOT_MENU_FLAG_ZIPL_OPTS 0x40
+
 static bool iplb_extended_needed(void *opaque)
 {
     S390IPLState *ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
@@ -219,6 +224,51 @@ static Property s390_ipl_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+static void s390_ipl_set_boot_menu(uint8_t *boot_menu_flags,
+                                   uint16_t *boot_menu_timeout)
+{
+    MachineState *machine = MACHINE(qdev_get_machine());
+    char *lp = object_property_get_str(OBJECT(machine), "loadparm", NULL);
+    QemuOptsList *plist = qemu_find_opts("boot-opts");
+    QemuOpts *opts = QTAILQ_FIRST(&plist->head);
+    const char *p = qemu_opt_get(opts, "menu");
+    unsigned long timeout = 0;
+
+    if (memcmp(lp, "PROMPT  ", 8) == 0) {
+        *boot_menu_flags = BOOT_MENU_FLAG_BOOT_OPTS;
+
+    } else if (*lp) {
+        /* If loadparm is set to any value, then discard boot menu */
+        return;
+
+    } else if (!p) {
+        /* In the absence of -boot menu, use zipl loader parameters */
+        *boot_menu_flags = BOOT_MENU_FLAG_ZIPL_OPTS;
+
+    } else if (strncmp(p, "on", 2) == 0) {
+        *boot_menu_flags = BOOT_MENU_FLAG_BOOT_OPTS;
+
+        p = qemu_opt_get(opts, "splash-time");
+
+        if (p && qemu_strtoul(p, NULL, 10, &timeout)) {
+            error_report("splash-time value is invalid, forcing it to 0.");
+            return;
+        }
+
+        /* Store timeout value as seconds */

hw/s390x/ipl.c static Property s390_ipl_properties[] = { · line 268

Quoted 10 lines
hw/s390x/ipl.c +10 -0
+        timeout /= 1000;
+
+        if (timeout > 0xffff) {
+            error_report("splash-time value is greater than 65535000,"
+                         " forcing it to 65535000.");
+            *boot_menu_timeout = 0xffff;
+            return;
+        }
+
+        *boot_menu_timeout = timeout;

I guess we likely need a cpu_to_be16() here?

hw/s390x/ipl.c static Property s390_ipl_properties[] = { · line 270

Quoted 2 lines
hw/s390x/ipl.c +2 -0
+    }
+}

Thomas

#18 lore raw

PATCH v2 4/5 s390-ccw: interactive boot menu for eckd dasd

In reply to Collin L. Walling

When the boot menu options are present and the guest's disk has been configured by the zipl tool, then the user will be presented with an interactive boot menu with labeled entries. An example of what the menu might look like:

zIPL v1.37.1-build-20170714 interactive boot menu.

0. default (linux-4.13.0)

1. linux-4.13.0
2. performance
3. kvm

Please choose (default will boot in 10 seconds):

If the user's input is empty or 0, the default zipl entry will be chosen. If the input is within the range presented by the menu, then the selection will be booted. Any erroneous input will cancel the timeout and prompt the user until correct input is given.

Any value set for loadparm will override all boot menu options. If loadparm=PROMPT, then the menu prompt will continuously wait until correct user input is given.

The absence of any boot options on the command line will attempt to use the zipl loader values.

---
 pc-bios/s390-ccw/Makefile   |   2 +-
 pc-bios/s390-ccw/bootmap.c  |  71 +++++++++++++-
 pc-bios/s390-ccw/bootmap.h  |   2 +
 pc-bios/s390-ccw/main.c     |   3 +
 pc-bios/s390-ccw/menu.c     | 223 ++++++++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/menu.h     |  28 ++++++
 pc-bios/s390-ccw/s390-ccw.h |   2 +
 pc-bios/s390-ccw/sclp.c     |  20 ++++
 pc-bios/s390-ccw/virtio.c   |   2 +-
 9 files changed, 348 insertions(+), 5 deletions(-)
 create mode 100644 pc-bios/s390-ccw/menu.c
 create mode 100644 pc-bios/s390-ccw/menu.h
pc-bios/s390-ccw/Makefile +1 -1
index 9f7904f..1712c2d 100644
@@ -9,7 +9,7 @@ $(call set-vpath, $(SRC_PATH)/pc-bios/s390-ccw)
 
 .PHONY : all clean build-all
 
-OBJECTS = start.o main.o bootmap.o sclp.o virtio.o virtio-scsi.o virtio-blkdev.o libc.o
+OBJECTS = start.o main.o bootmap.o sclp.o virtio.o virtio-scsi.o virtio-blkdev.o libc.o menu.o
 QEMU_CFLAGS := $(filter -W%, $(QEMU_CFLAGS))
 QEMU_CFLAGS += -ffreestanding -fno-delete-null-pointer-checks -msoft-float
 QEMU_CFLAGS += -march=z900 -fPIE -fno-strict-aliasing
pc-bios/s390-ccw/bootmap.c +68 -3

125 lines not on this page · raw message

pc-bios/s390-ccw/bootmap.h +2 -0
index b700d08..8089402 100644
@@ -74,6 +74,7 @@ typedef struct ScsiMbr {
 } __attribute__ ((packed)) ScsiMbr;
 
 #define ZIPL_MAGIC              "zIPL"
+#define ZIPL_MAGIC_EBCDIC       "\xa9\xc9\xd7\xd3"
 #define IPL1_MAGIC "\xc9\xd7\xd3\xf1" /* == "IPL1" in EBCDIC */
 #define IPL2_MAGIC "\xc9\xd7\xd3\xf2" /* == "IPL2" in EBCDIC */
 #define VOL1_MAGIC "\xe5\xd6\xd3\xf1" /* == "VOL1" in EBCDIC */
@@ -229,6 +230,7 @@ typedef struct BootInfo {          /* @ 0x70, record #0    */
 /*
  * Structs for IPL
  */
+#define STAGE2_MAX_SIZE     0x3000
 #define STAGE2_BLK_CNT_MAX  24 /* Stage 1b can load up to 24 blocks */
 
 typedef struct EckdCdlIpl1 {
pc-bios/s390-ccw/main.c +3 -0
index a8ef120..fb0ef92 100644
@@ -11,6 +11,7 @@
 #include "libc.h"
 #include "s390-ccw.h"
 #include "virtio.h"
+#include "menu.h"
 
 char stack[PAGE_SIZE * 8] __attribute__((__aligned__(PAGE_SIZE)));
 static SubChannelId blk_schid = { .one = 1 };
@@ -101,6 +102,8 @@ static void virtio_setup(void)
             blk_schid.ssid = iplb.ccw.ssid & 0x3;
             debug_print_int("ssid ", blk_schid.ssid);
             found = find_dev(&schib, dev_no);
+            menu_set_parms(iplb.ccw.boot_menu_flags,
+                           iplb.ccw.boot_menu_timeout);
             break;
         case S390_IPL_TYPE_QEMU_SCSI:
             vdev->scsi_device_selected = true;
pc-bios/s390-ccw/menu.c new file +223 -0

223 lines not on this page · raw message

pc-bios/s390-ccw/menu.h new file +28 -0
new file mode 100644
index 0000000..a8727fa
@@ -0,0 +1,28 @@
+/*
+ * QEMU S390 Interactive Boot Menu
+ *
+ * Copyright 2017 IBM Corp.
+ * Author: Collin L. Walling <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or (at
+ * your option) any later version. See the COPYING file in the top-level
+ * directory.
+ */
+
+#ifndef MENU_H
+#define MENU_H
+
+#define BOOT_MENU_FLAG_BOOT_OPTS 0x80
+#define BOOT_MENU_FLAG_ZIPL_OPTS 0x40
+
+typedef struct ZiplParms {
+    unsigned short flag;
+    unsigned short timeout;
+    unsigned long long menu_start;
+} ZiplParms;
+
+void menu_set_parms(uint8_t boot_menu_flags, uint16_t boot_menu_timeout);
+bool menu_check_flags(uint8_t check_flags);
+int menu_get_zipl_boot_index(const void *stage2, ZiplParms zipl_parms);
+
+#endif /* MENU_H */
pc-bios/s390-ccw/s390-ccw.h +2 -0
index 25d4d21..df4bc88 100644
@@ -71,6 +71,7 @@ unsigned int get_loadparm_index(void);
 void sclp_print(const char *string);
 void sclp_setup(void);
 void sclp_get_loadparm_ascii(char *loadparm);
+void sclp_read(char *str, size_t len);
 
 /* virtio.c */
 unsigned long virtio_load_direct(ulong rec_list1, ulong rec_list2,
@@ -79,6 +80,7 @@ bool virtio_is_supported(SubChannelId schid);
 void virtio_blk_setup_device(SubChannelId schid);
 int virtio_read(ulong sector, void *load_addr);
 int enable_mss_facility(void);
+u64 get_clock(void);
 ulong get_second(void);
 
 /* bootmap.c */
pc-bios/s390-ccw/sclp.c +20 -0
index 486fce1..5e4a78b 100644
@@ -101,3 +101,23 @@ void sclp_get_loadparm_ascii(char *loadparm)
         ebcdic_to_ascii((char *) sccb->loadparm, loadparm, 8);
     }
 }
+
+void sclp_read(char *str, size_t len)
+{
+    ReadEventData *sccb = (void *)_sccb;
+    char *buf = (char *)(&sccb->ebh) + 7;
+
+    /* Len should not exceed the maximum size of the event buffer */
+    if (len > SCCB_SIZE - 8) {
+        len = SCCB_SIZE - 8;
+    }
+
+    sccb->h.length = SCCB_SIZE;
+    sccb->h.function_code = SCLP_UNCONDITIONAL_READ;
+    sccb->ebh.length = sizeof(EventBufferHeader);
+    sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
+    sccb->ebh.flags = 0;
+
+    sclp_service_call(SCLP_CMD_READ_EVENT_DATA, sccb);
+    memcpy(str, buf, len);
+}
pc-bios/s390-ccw/virtio.c +1 -1
index c890a03..817e7f5 100644
@@ -176,7 +176,7 @@ void vring_send_buf(VRing *vr, void *p, int len, int flags)
     }
 }
 
-static u64 get_clock(void)
+u64 get_clock(void)
 {
     u64 r;
 
Signature · 1 line
2.7.4
#19 lore raw

Re: [Qemu-devel] [PATCH v2 4/5] s390-ccw: interactive boot menu for eckd dasd

Quoted 208 lines · Collin L. Walling

When the boot menu options are present and the guest's disk has been configured by the zipl tool, then the user will be presented with an interactive boot menu with labeled entries. An example of what the menu might look like:

zIPL v1.37.1-build-20170714 interactive boot menu.

0. default (linux-4.13.0)

1. linux-4.13.0
2. performance
3. kvm

Please choose (default will boot in 10 seconds):

If the user's input is empty or 0, the default zipl entry will be chosen. If the input is within the range presented by the menu, then the selection will be booted. Any erroneous input will cancel the timeout and prompt the user until correct input is given.

Any value set for loadparm will override all boot menu options. If loadparm=PROMPT, then the menu prompt will continuously wait until correct user input is given.

The absence of any boot options on the command line will attempt to use the zipl loader values.

---
  pc-bios/s390-ccw/Makefile   |   2 +-
  pc-bios/s390-ccw/bootmap.c  |  71 +++++++++++++-
  pc-bios/s390-ccw/bootmap.h  |   2 +
  pc-bios/s390-ccw/main.c     |   3 +
  pc-bios/s390-ccw/menu.c     | 223 ++++++++++++++++++++++++++++++++++++++++++++
  pc-bios/s390-ccw/menu.h     |  28 ++++++
  pc-bios/s390-ccw/s390-ccw.h |   2 +
  pc-bios/s390-ccw/sclp.c     |  20 ++++
  pc-bios/s390-ccw/virtio.c   |   2 +-
  9 files changed, 348 insertions(+), 5 deletions(-)

create mode 100644 pc-bios/s390-ccw/menu.c
create mode 100644 pc-bios/s390-ccw/menu.h

pc-bios/s390-ccw/Makefile +1 -1
index 9f7904f..1712c2d 100644
@@ -9,7 +9,7 @@ $(call set-vpath, $(SRC_PATH)/pc-bios/s390-ccw)
 
  .PHONY : all clean build-all
 
-OBJECTS = start.o main.o bootmap.o sclp.o virtio.o virtio-scsi.o virtio-blkdev.o libc.o
+OBJECTS = start.o main.o bootmap.o sclp.o virtio.o virtio-scsi.o virtio-blkdev.o libc.o menu.o
  QEMU_CFLAGS := $(filter -W%, $(QEMU_CFLAGS))
  QEMU_CFLAGS += -ffreestanding -fno-delete-null-pointer-checks -msoft-float
  QEMU_CFLAGS += -march=z900 -fPIE -fno-strict-aliasing
pc-bios/s390-ccw/bootmap.c +68 -3
index 5546b79..c817cf8 100644
@@ -13,6 +13,7 @@
  #include "bootmap.h"
  #include "virtio.h"
  #include "bswap.h"
+#include "menu.h"
 
  #ifdef DEBUG
  /* #define DEBUG_FALLBACK */
@@ -83,6 +84,7 @@ static void jump_to_IPL_code(uint64_t address)
 
  static unsigned char _bprs[8*1024]; /* guessed "max" ECKD sector size */
  static const int max_bprs_entries = sizeof(_bprs) / sizeof(ExtEckdBlockPtr);
+static uint8_t stage2[STAGE2_MAX_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
 
  static inline void verify_boot_info(BootInfo *bip)
  {
@@ -182,7 +184,57 @@ static block_number_t load_eckd_segments(block_number_t blk, uint64_t *address)
      return block_nr;
  }
 
-static void run_eckd_boot_script(block_number_t mbr_block_nr)
+static void read_stage2(block_number_t s1b_block_nr)
+{
+    block_number_t s2_block_nr;
+    EckdStage1b *s1b = (void *)sec;
+    int i;
+
+    /* Get Stage1b data */
+    memset(sec, FREE_SPACE_FILLER, sizeof(sec));
+    read_block(s1b_block_nr, s1b, "Cannot read stage1b boot loader.");
+
+    /* Get Stage2 data */
+    memset(stage2, FREE_SPACE_FILLER, sizeof(stage2));
+
+    for (i = 0; i < STAGE2_MAX_SIZE / MAX_SECTOR_SIZE; i++) {
+        s2_block_nr = eckd_block_num((void *)&(s1b->seek[i].cyl));
+
+        if (!s2_block_nr) {
+            break;
+        }
+
+        read_block(s2_block_nr, (stage2 + MAX_SECTOR_SIZE * i),
+                   "Error reading Stage2 data");
+    }
+}
+
+static bool find_zipl_boot_menu_data(block_number_t s1b_block_nr,
+                                     ZiplParms *zipl_parms)
+{
+    int offset;
+    void *s2_offset;
+
+    read_stage2(s1b_block_nr);
+
+    /* Menu banner starts with "zIPL" */
+    for (offset = 0; offset < STAGE2_MAX_SIZE - 4; offset++) {
+        s2_offset = stage2 + offset;
+
+        if (magic_match(s2_offset, ZIPL_MAGIC_EBCDIC)) {
+            zipl_parms->flag = *(uint16_t *)(s2_offset - 140);
+            zipl_parms->timeout = *(uint16_t *)(s2_offset - 138);
+            zipl_parms->menu_start = offset;
+            return true;
+        }
+    }
+
+    sclp_print("No zipl boot menu data found. Booting default entry.");
+    return false;
+}
+
+static void run_eckd_boot_script(block_number_t mbr_block_nr,
+                                 block_number_t s1b_block_nr)
  {
      int i;
      unsigned int loadparm = get_loadparm_index();
@@ -190,6 +242,12 @@ static void run_eckd_boot_script(block_number_t mbr_block_nr)
      uint64_t address;
      ScsiMbr *bte = (void *)sec; /* Eckd bootmap table entry */
      BootMapScript *bms = (void *)sec;
+    ZiplParms zipl_parms;
+
+    if (menu_check_flags(BOOT_MENU_FLAG_BOOT_OPTS | BOOT_MENU_FLAG_ZIPL_OPTS)
+        && find_zipl_boot_menu_data(s1b_block_nr, &zipl_parms)) {
+        loadparm = menu_get_zipl_boot_index(stage2, zipl_parms);
+    }
 
      debug_print_int("loadparm", loadparm);
      IPL_assert(loadparm < 31, "loadparm value greater than"
@@ -224,6 +282,7 @@ static void ipl_eckd_cdl(void)
      EckdCdlIpl2 *ipl2 = (void *)sec;
      IplVolumeLabel *vlbl = (void *)sec;
      block_number_t mbr_block_nr;
+    block_number_t s1b_block_nr;
 
      /* we have just read the block #0 and recognized it as "IPL1" */
      sclp_print("CDL\n");
@@ -241,6 +300,9 @@ static void ipl_eckd_cdl(void)
      /* save pointer to Boot Script */
      mbr_block_nr = eckd_block_num((void *)&(mbr->blockptr));
 
+    /* save pointer to Stage1b Data */
+    s1b_block_nr = eckd_block_num((void *)&(ipl2->stage1.seek[0].cyl));
+
      memset(sec, FREE_SPACE_FILLER, sizeof(sec));
      read_block(2, vlbl, "Cannot read Volume Label at block 2");
      IPL_assert(magic_match(vlbl->key, VOL1_MAGIC),
@@ -249,7 +311,7 @@ static void ipl_eckd_cdl(void)
                 "Invalid magic of volser block");
      print_volser(vlbl->f.volser);
 
-    run_eckd_boot_script(mbr_block_nr);
+    run_eckd_boot_script(mbr_block_nr, s1b_block_nr);
      /* no return */
  }
 
@@ -281,6 +343,7 @@ static void print_eckd_ldl_msg(ECKD_IPL_mode_t mode)
  static void ipl_eckd_ldl(ECKD_IPL_mode_t mode)
  {
      block_number_t mbr_block_nr;
+    block_number_t s1b_block_nr;
      EckdLdlIpl1 *ipl1 = (void *)sec;
 
      if (mode != ECKD_LDL_UNLABELED) {
@@ -302,7 +365,9 @@ static void ipl_eckd_ldl(ECKD_IPL_mode_t mode)
      mbr_block_nr =
          eckd_block_num((void *)&(ipl1->boot_info.bp.ipl.bm_ptr.eckd.bptr));
 
-    run_eckd_boot_script(mbr_block_nr);
+    s1b_block_nr = eckd_block_num((void *)&(ipl1->stage1.seek[0].cyl));
+
+    run_eckd_boot_script(mbr_block_nr, s1b_block_nr);
      /* no return */
  }
 
pc-bios/s390-ccw/bootmap.h +1 -0
index b700d08..8089402 100644
@@ -74,6 +74,7 @@ typedef struct ScsiMbr {
  } __attribute__ ((packed)) ScsiMbr;
 
  #define ZIPL_MAGIC              "zIPL"
+#define ZIPL_MAGIC_EBCDIC       "\xa9\xc9\xd7\xd3"
  #define IPL1_MAGIC "\xc9\xd7\xd3\xf1" /* == "IPL1" in EBCDIC */
  #define IPL2_MAGIC "\xc9\xd7\xd3\xf2" /* == "IPL2" in EBCDIC */
  #define VOL1_MAGIC "\xe5\xd6\xd3\xf1" /* == "VOL1" in EBCDIC */

pc-bios/s390-ccw/bootmap.h typedef struct BootInfo { /* @ 0x70, record #0 */ · line 233

Quoted 4 lines
pc-bios/s390-ccw/bootmap.h +1 -0
@@ -229,6 +230,7 @@ typedef struct BootInfo {          /* @ 0x70, record #0    */
  /*
   * Structs for IPL
   */
+#define STAGE2_MAX_SIZE     0x3000

Is there a reason this is in hex?

Quoted 158 lines
pc-bios/s390-ccw/bootmap.h +0 -0
  #define STAGE2_BLK_CNT_MAX  24 /* Stage 1b can load up to 24 blocks */
 
  typedef struct EckdCdlIpl1 {
pc-bios/s390-ccw/main.c +3 -0
index a8ef120..fb0ef92 100644
@@ -11,6 +11,7 @@
  #include "libc.h"
  #include "s390-ccw.h"
  #include "virtio.h"
+#include "menu.h"
 
  char stack[PAGE_SIZE * 8] __attribute__((__aligned__(PAGE_SIZE)));
  static SubChannelId blk_schid = { .one = 1 };
@@ -101,6 +102,8 @@ static void virtio_setup(void)
              blk_schid.ssid = iplb.ccw.ssid & 0x3;
              debug_print_int("ssid ", blk_schid.ssid);
              found = find_dev(&schib, dev_no);
+            menu_set_parms(iplb.ccw.boot_menu_flags,
+                           iplb.ccw.boot_menu_timeout);
              break;
          case S390_IPL_TYPE_QEMU_SCSI:
              vdev->scsi_device_selected = true;
pc-bios/s390-ccw/menu.c new file +128 -0
new file mode 100644
index 0000000..d707afb
@@ -0,0 +1,223 @@
+/*
+ * QEMU S390 Interactive Boot Menu
+ *
+ * Copyright 2017 IBM Corp.
+ * Author: Collin L. Walling <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or (at
+ * your option) any later version. See the COPYING file in the top-level
+ * directory.
+ */
+
+#include "libc.h"
+#include "s390-ccw.h"
+#include "menu.h"
+
+#define KEYCODE_NO_INP '\0'
+#define KEYCODE_ESCAPE '\033'
+#define KEYCODE_BACKSP '\177'
+#define KEYCODE_ENTER  '\r'
+
+static uint8_t flags;
+static uint64_t timeout;
+
+static inline void enable_clock_int(void)
+{
+    uint64_t tmp = 0;
+
+    asm volatile(
+        "stctg      0,0,%0\n"
+        "oi         6+%0, 0x8\n"
+        "lctlg      0,0,%0"
+        : : "Q" (tmp)
+    );
+}
+
+static inline void disable_clock_int(void)
+{
+    uint64_t tmp = 0;
+
+    asm volatile(
+        "stctg      0,0,%0\n"
+        "ni         6+%0, 0xf7\n"
+        "lctlg      0,0,%0"
+        : : "Q" (tmp)
+    );
+}
+
+static inline void set_clock_comparator(uint64_t time)
+{
+    asm volatile("sckc %0" : : "Q" (time));
+}
+
+static inline bool check_clock_int(void)
+{
+    uint16_t code = *(uint16_t *)0x86;
+
+    consume_sclp_int();
+
+    return code == 0x1004;
+}
+
+static int read_prompt(char *buf, size_t len)
+{
+    char inp[2];
+    uint8_t idx = 0;
+    uint64_t time;
+
+    if (timeout) {
+        time = get_clock() + (timeout << 32);
+        set_clock_comparator(time);
+        enable_clock_int();
+    }
+
+    inp[1] = '\0';
+
+    while (!check_clock_int()) {
+
+        /* Process only one character at a time */
+        sclp_read(inp, 1);
+
+        switch (inp[0]) {
+        case KEYCODE_NO_INP:
+        case KEYCODE_ESCAPE:
+            continue;
+        case KEYCODE_BACKSP:
+            if (idx > 0) {
+                /* Remove last character */
+                buf[idx - 1] = ' ';
+                sclp_print("\r");
+                sclp_print(buf);
+
+                idx--;
+
+                /* Reset cursor */
+                buf[idx] = 0;
+                sclp_print("\r");
+                sclp_print(buf);
+            }
+            continue;
+        case KEYCODE_ENTER:
+            disable_clock_int();
+            return idx;
+        }
+
+        /* Echo input and add to buffer */
+        if (idx < len) {
+            buf[idx] = inp[0];
+            sclp_print(inp);
+            idx++;
+        }
+    }
+
+    disable_clock_int();
+    *buf = NULL;
+
+    return 0;
+}
+
+static int get_index(void)
+{
+    char buf[10];
+    int len;
+    int i;
+
+    memset(buf, 0, sizeof(buf));
+
+    len = read_prompt(buf, sizeof(buf));
+

pc-bios/s390-ccw/menu.c line 138

Quoted 10 lines
pc-bios/s390-ccw/menu.c new file +10 -0
+    if (len == 0) {
+        return 0;
+    }
+
+    for (i = 0; i < len; i++) {
+        if (!isdigit(buf[i])) {
+            return -1;
+        }
+    }
+

doesn't the atoi function already do the isdigit check? Is there a reason we are doing it here again?

Quoted 43 lines
pc-bios/s390-ccw/menu.c new file +43 -0
+    return atoi(buf);
+}
+
+static int get_boot_index(int entries)
+{
+    char tmp[6];
+    int boot_index;
+
+    /* Prompt User */
+    if (timeout > 0) {
+        sclp_print("Please choose (default will boot in ");
+        sclp_print(itostr(timeout, tmp, sizeof(tmp)));
+        sclp_print(" seconds):\n");
+    } else {
+        sclp_print("Please choose:\n");
+    }
+
+    /* Get Menu Choice */
+    boot_index = get_index();
+
+    timeout = 0;
+
+    while (boot_index < 0 || boot_index >= entries) {
+        sclp_print("\nError: undefined configuration"
+                   "\nPlease choose:\n");
+        boot_index = get_index();
+    }
+
+    sclp_print("\nBooting entry #");
+    sclp_print(itostr(boot_index, tmp, sizeof(tmp)));
+
+    return boot_index;
+}
+
+static void zipl_println(const char *data, size_t len)
+{
+    char buf[len + 1];
+
+    ebcdic_to_ascii(data, buf, len);
+    buf[len] = '\n';
+    buf[len + 1] = '\0';
+
+    sclp_print(buf);

pc-bios/s390-ccw/menu.c line 191

Quoted 10 lines
pc-bios/s390-ccw/menu.c new file +10 -0
+}
+
+int menu_get_zipl_boot_index(const void *stage2, ZiplParms zipl_parms)
+{
+    const char *data = stage2 + zipl_parms.menu_start;
+    size_t len;
+    int ct;
+
+    if (flags & BOOT_MENU_FLAG_ZIPL_OPTS) {
+        if (zipl_parms.flag) {

should we not check for zipl_parms.timeout? or do we know that if flags is set than timeout will always be set?

Quoted 127 lines
pc-bios/s390-ccw/menu.c new file +32 -0
+            timeout = zipl_parms.timeout;
+        } else {
+            return 0; /* Boot default */
+        }
+    }
+
+    /* Print and count all menu items, including the banner */
+    for (ct = 0; *data; ct++) {
+        len = strlen(data);
+        zipl_println(data, len);
+        data += len + 1;
+
+        if (ct < 2) {
+            sclp_print("\n");
+        }
+    }
+
+    sclp_print("\n");
+
+    return get_boot_index(ct - 1);
+}
+
+void menu_set_parms(uint8_t boot_menu_flag, uint16_t boot_menu_timeout)
+{
+    flags = boot_menu_flag;
+    timeout = boot_menu_timeout;
+}
+
+int menu_check_flags(uint8_t check_flags)
+{
+    return flags & check_flags;
+}
pc-bios/s390-ccw/menu.h new file +28 -0
new file mode 100644
index 0000000..a8727fa
@@ -0,0 +1,28 @@
+/*
+ * QEMU S390 Interactive Boot Menu
+ *
+ * Copyright 2017 IBM Corp.
+ * Author: Collin L. Walling <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or (at
+ * your option) any later version. See the COPYING file in the top-level
+ * directory.
+ */
+
+#ifndef MENU_H
+#define MENU_H
+
+#define BOOT_MENU_FLAG_BOOT_OPTS 0x80
+#define BOOT_MENU_FLAG_ZIPL_OPTS 0x40
+
+typedef struct ZiplParms {
+    unsigned short flag;
+    unsigned short timeout;
+    unsigned long long menu_start;
+} ZiplParms;
+
+void menu_set_parms(uint8_t boot_menu_flags, uint16_t boot_menu_timeout);
+bool menu_check_flags(uint8_t check_flags);
+int menu_get_zipl_boot_index(const void *stage2, ZiplParms zipl_parms);
+
+#endif /* MENU_H */
pc-bios/s390-ccw/s390-ccw.h +2 -0
index 25d4d21..df4bc88 100644
@@ -71,6 +71,7 @@ unsigned int get_loadparm_index(void);
  void sclp_print(const char *string);
  void sclp_setup(void);
  void sclp_get_loadparm_ascii(char *loadparm);
+void sclp_read(char *str, size_t len);
 
  /* virtio.c */
  unsigned long virtio_load_direct(ulong rec_list1, ulong rec_list2,
@@ -79,6 +80,7 @@ bool virtio_is_supported(SubChannelId schid);
  void virtio_blk_setup_device(SubChannelId schid);
  int virtio_read(ulong sector, void *load_addr);
  int enable_mss_facility(void);
+u64 get_clock(void);
  ulong get_second(void);
 
  /* bootmap.c */
pc-bios/s390-ccw/sclp.c +20 -0
index 486fce1..5e4a78b 100644
@@ -101,3 +101,23 @@ void sclp_get_loadparm_ascii(char *loadparm)
          ebcdic_to_ascii((char *) sccb->loadparm, loadparm, 8);
      }
  }
+
+void sclp_read(char *str, size_t len)
+{
+    ReadEventData *sccb = (void *)_sccb;
+    char *buf = (char *)(&sccb->ebh) + 7;
+
+    /* Len should not exceed the maximum size of the event buffer */
+    if (len > SCCB_SIZE - 8) {
+        len = SCCB_SIZE - 8;
+    }
+
+    sccb->h.length = SCCB_SIZE;
+    sccb->h.function_code = SCLP_UNCONDITIONAL_READ;
+    sccb->ebh.length = sizeof(EventBufferHeader);
+    sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
+    sccb->ebh.flags = 0;
+
+    sclp_service_call(SCLP_CMD_READ_EVENT_DATA, sccb);
+    memcpy(str, buf, len);
+}
pc-bios/s390-ccw/virtio.c +1 -1
index c890a03..817e7f5 100644
@@ -176,7 +176,7 @@ void vring_send_buf(VRing *vr, void *p, int len, int flags)
      }
  }
 
-static u64 get_clock(void)
+u64 get_clock(void)
  {
      u64 r;
 
#20 lore raw

Re: [Qemu-devel] [qemu-s390x] [PATCH v2 4/5] s390-ccw: interactive boot menu for eckd dasd

Quoted 213 lines · Farhan Ali
Quoted 210 lines · Collin L. Walling

When the boot menu options are present and the guest's disk has been configured by the zipl tool, then the user will be presented with an interactive boot menu with labeled entries. An example of what the menu might look like:

     zIPL v1.37.1-build-20170714 interactive boot menu.

      0. default (linux-4.13.0)

      1. linux-4.13.0
      2. performance
      3. kvm

     Please choose (default will boot in 10 seconds):

If the user's input is empty or 0, the default zipl entry will be chosen. If the input is within the range presented by the menu, then the selection will be booted. Any erroneous input will cancel the timeout and prompt the user until correct input is given.

Any value set for loadparm will override all boot menu options. If loadparm=PROMPT, then the menu prompt will continuously wait until correct user input is given.

The absence of any boot options on the command line will attempt to use the zipl loader values.

---
  pc-bios/s390-ccw/Makefile   |   2 +- pc-bios/s390-ccw/bootmap.c  |  71 +++++++++++++- pc-bios/s390-ccw/bootmap.h  |   2 + pc-bios/s390-ccw/main.c     |   3 + pc-bios/s390-ccw/menu.c     | 223
++++++++++++++++++++++++++++++++++++++++++++
  pc-bios/s390-ccw/menu.h     |  28 ++++++ pc-bios/s390-ccw/s390-ccw.h |   2 + pc-bios/s390-ccw/sclp.c     |  20 ++++ pc-bios/s390-ccw/virtio.c   |   2 +-

  9 files changed, 348 insertions(+), 5 deletions(-)

  create mode 100644 pc-bios/s390-ccw/menu.c
  create mode 100644 pc-bios/s390-ccw/menu.h

pc-bios/s390-ccw/Makefile +0 -0
index 9f7904f..1712c2d 100644
@@ -9,7 +9,7 @@ $(call set-vpath, $(SRC_PATH)/pc-bios/s390-ccw)
 

  .PHONY : all clean build-all

-OBJECTS = start.o main.o bootmap.o sclp.o virtio.o virtio-scsi.o virtio-blkdev.o libc.o
+OBJECTS = start.o main.o bootmap.o sclp.o virtio.o virtio-scsi.o virtio-blkdev.o libc.o menu.o
  QEMU_CFLAGS := $(filter -W%, $(QEMU_CFLAGS)) QEMU_CFLAGS += -ffreestanding -fno-delete-null-pointer-checks -msoft-float
  QEMU_CFLAGS += -march=z900 -fPIE -fno-strict-aliasing

pc-bios/s390-ccw/bootmap.c +1 -0
index 5546b79..c817cf8 100644
@@ -13,6 +13,7 @@
   #include "bootmap.h"
   #include "virtio.h"
   #include "bswap.h"
+#include "menu.h"
 
   #ifdef DEBUG
   /* #define DEBUG_FALLBACK */
@@ -83,6 +84,7 @@ static void jump_to_IPL_code(uint64_t address)
 

  static unsigned char _bprs[8*1024]; /* guessed "max" ECKD sector size */ static const int max_bprs_entries = sizeof(_bprs) / sizeof(ExtEckdBlockPtr); +static uint8_t stage2[STAGE2_MAX_SIZE] __attribute__((__aligned__(PAGE_SIZE)));

  static inline void verify_boot_info(BootInfo *bip) {

pc-bios/s390-ccw/bootmap.c +51 -1
@@ -182,7 +184,57 @@ static block_number_t 
 load_eckd_segments(block_number_t blk, uint64_t *address)
       return block_nr;
   }
 
-static void run_eckd_boot_script(block_number_t mbr_block_nr)
+static void read_stage2(block_number_t s1b_block_nr)
+{
+    block_number_t s2_block_nr;
+    EckdStage1b *s1b = (void *)sec;
+    int i;
+
+    /* Get Stage1b data */
+    memset(sec, FREE_SPACE_FILLER, sizeof(sec));
+    read_block(s1b_block_nr, s1b, "Cannot read stage1b boot loader.");
+
+    /* Get Stage2 data */
+    memset(stage2, FREE_SPACE_FILLER, sizeof(stage2));
+
+    for (i = 0; i < STAGE2_MAX_SIZE / MAX_SECTOR_SIZE; i++) {
+        s2_block_nr = eckd_block_num((void *)&(s1b->seek[i].cyl));
+
+        if (!s2_block_nr) {
+            break;
+        }
+
+        read_block(s2_block_nr, (stage2 + MAX_SECTOR_SIZE * i),
+                   "Error reading Stage2 data");
+    }
+}
+
+static bool find_zipl_boot_menu_data(block_number_t s1b_block_nr,
+                                     ZiplParms *zipl_parms)
+{
+    int offset;
+    void *s2_offset;
+
+    read_stage2(s1b_block_nr);
+
+    /* Menu banner starts with "zIPL" */
+    for (offset = 0; offset < STAGE2_MAX_SIZE - 4; offset++) {
+        s2_offset = stage2 + offset;
+
+        if (magic_match(s2_offset, ZIPL_MAGIC_EBCDIC)) {
+            zipl_parms->flag = *(uint16_t *)(s2_offset - 140);
+            zipl_parms->timeout = *(uint16_t *)(s2_offset - 138);
+            zipl_parms->menu_start = offset;
+            return true;
+        }
+    }
+
+    sclp_print("No zipl boot menu data found. Booting default entry.");
+    return false;
+}
+
+static void run_eckd_boot_script(block_number_t mbr_block_nr,
+                                 block_number_t s1b_block_nr)
   {
       int i;

      unsigned int loadparm = get_loadparm_index();

pc-bios/s390-ccw/bootmap.c +0 -0
@@ -190,6 +242,12 @@ static void run_eckd_boot_script(block_number_t 
 mbr_block_nr)
       uint64_t address;

      ScsiMbr *bte = (void *)sec; /* Eckd bootmap table entry */ BootMapScript *bms = (void *)sec;
+    ZiplParms zipl_parms;
+
+    if (menu_check_flags(BOOT_MENU_FLAG_BOOT_OPTS | BOOT_MENU_FLAG_ZIPL_OPTS)
+        && find_zipl_boot_menu_data(s1b_block_nr, &zipl_parms)) { +        loadparm = menu_get_zipl_boot_index(stage2, zipl_parms); +    }

      debug_print_int("loadparm", loadparm); IPL_assert(loadparm < 31, "loadparm value greater than"

pc-bios/s390-ccw/bootmap.c +6 -1
@@ -224,6 +282,7 @@ static void ipl_eckd_cdl(void)
       EckdCdlIpl2 *ipl2 = (void *)sec;
       IplVolumeLabel *vlbl = (void *)sec;
       block_number_t mbr_block_nr;
+    block_number_t s1b_block_nr;
 
       /* we have just read the block #0 and recognized it as "IPL1" */
       sclp_print("CDL\n");
@@ -241,6 +300,9 @@ static void ipl_eckd_cdl(void)
       /* save pointer to Boot Script */
       mbr_block_nr = eckd_block_num((void *)&(mbr->blockptr));
 
+    /* save pointer to Stage1b Data */
+    s1b_block_nr = eckd_block_num((void *)&(ipl2->stage1.seek[0].cyl));
+
       memset(sec, FREE_SPACE_FILLER, sizeof(sec));
       read_block(2, vlbl, "Cannot read Volume Label at block 2");
       IPL_assert(magic_match(vlbl->key, VOL1_MAGIC),
@@ -249,7 +311,7 @@ static void ipl_eckd_cdl(void)
                  "Invalid magic of volser block");
       print_volser(vlbl->f.volser);
 
-    run_eckd_boot_script(mbr_block_nr);
+    run_eckd_boot_script(mbr_block_nr, s1b_block_nr);
       /* no return */
   }
 
@@ -281,6 +343,7 @@ static void print_eckd_ldl_msg(ECKD_IPL_mode_t mode)
   static void ipl_eckd_ldl(ECKD_IPL_mode_t mode)
   {
       block_number_t mbr_block_nr;
+    block_number_t s1b_block_nr;
       EckdLdlIpl1 *ipl1 = (void *)sec;
 
       if (mode != ECKD_LDL_UNLABELED) {
@@ -302,7 +365,9 @@ static void ipl_eckd_ldl(ECKD_IPL_mode_t mode)

      mbr_block_nr =
          eckd_block_num((void *)&(ipl1->boot_info.bp.ipl.bm_ptr.eckd.bptr));

-    run_eckd_boot_script(mbr_block_nr);
+    s1b_block_nr = eckd_block_num((void *)&(ipl1->stage1.seek[0].cyl)); +
+    run_eckd_boot_script(mbr_block_nr, s1b_block_nr);
      /* no return */
  }

pc-bios/s390-ccw/bootmap.h +0 -0
index b700d08..8089402 100644
@@ -74,6 +74,7 @@ typedef struct ScsiMbr {
   } __attribute__ ((packed)) ScsiMbr;
 
Quoted 12 lines

pc-bios/s390-ccw/bootmap.h typedef struct BootInfo { /* @ 0x70, record #0    */ · line 233

Quoted 10 lines
pc-bios/s390-ccw/bootmap.h +2 -0
   #define ZIPL_MAGIC              "zIPL"
+#define ZIPL_MAGIC_EBCDIC       "\xa9\xc9\xd7\xd3"
   #define IPL1_MAGIC "\xc9\xd7\xd3\xf1" /* == "IPL1" in EBCDIC */
   #define IPL2_MAGIC "\xc9\xd7\xd3\xf2" /* == "IPL2" in EBCDIC */
   #define VOL1_MAGIC "\xe5\xd6\xd3\xf1" /* == "VOL1" in EBCDIC */
@@ -229,6 +230,7 @@ typedef struct BootInfo { /* @ 0x70, record #0    */
   /*
    * Structs for IPL
    */
+#define STAGE2_MAX_SIZE     0x3000

Is there a reason this is in hex?

I suspect that if the stage2 size were to grow in the future, it will be by a multiple of one sector size.  Having this value in hex is a little more sane to update than if it were in decimal.

Quoted 166 lines
Quoted 166 lines
pc-bios/s390-ccw/bootmap.h +0 -0
   #define STAGE2_BLK_CNT_MAX  24 /* Stage 1b can load up to 24 blocks */
 
   typedef struct EckdCdlIpl1 {
pc-bios/s390-ccw/main.c +3 -0
index a8ef120..fb0ef92 100644
@@ -11,6 +11,7 @@
   #include "libc.h"
   #include "s390-ccw.h"
   #include "virtio.h"
+#include "menu.h"
 
   char stack[PAGE_SIZE * 8] __attribute__((__aligned__(PAGE_SIZE)));
   static SubChannelId blk_schid = { .one = 1 };
@@ -101,6 +102,8 @@ static void virtio_setup(void)
               blk_schid.ssid = iplb.ccw.ssid & 0x3;
               debug_print_int("ssid ", blk_schid.ssid);
               found = find_dev(&schib, dev_no);
+            menu_set_parms(iplb.ccw.boot_menu_flags,
+ iplb.ccw.boot_menu_timeout);
               break;

          case S390_IPL_TYPE_QEMU_SCSI:
              vdev->scsi_device_selected = true;

pc-bios/s390-ccw/menu.c new file +7 -0
new file mode 100644
index 0000000..d707afb
@@ -0,0 +1,223 @@
+/*
+ * QEMU S390 Interactive Boot Menu
+ *
+ * Copyright 2017 IBM Corp.
+ * Author: Collin L. Walling <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 

or (at
+ * your option) any later version. See the COPYING file in the top-level
+ * directory.
+ */

pc-bios/s390-ccw/menu.c new file +123 -0
+
+#include "libc.h"
+#include "s390-ccw.h"
+#include "menu.h"
+
+#define KEYCODE_NO_INP '\0'
+#define KEYCODE_ESCAPE '\033'
+#define KEYCODE_BACKSP '\177'
+#define KEYCODE_ENTER  '\r'
+
+static uint8_t flags;
+static uint64_t timeout;
+
+static inline void enable_clock_int(void)
+{
+    uint64_t tmp = 0;
+
+    asm volatile(
+        "stctg      0,0,%0\n"
+        "oi         6+%0, 0x8\n"
+        "lctlg      0,0,%0"
+        : : "Q" (tmp)
+    );
+}
+
+static inline void disable_clock_int(void)
+{
+    uint64_t tmp = 0;
+
+    asm volatile(
+        "stctg      0,0,%0\n"
+        "ni         6+%0, 0xf7\n"
+        "lctlg      0,0,%0"
+        : : "Q" (tmp)
+    );
+}
+
+static inline void set_clock_comparator(uint64_t time)
+{
+    asm volatile("sckc %0" : : "Q" (time));
+}
+
+static inline bool check_clock_int(void)
+{
+    uint16_t code = *(uint16_t *)0x86;
+
+    consume_sclp_int();
+
+    return code == 0x1004;
+}
+
+static int read_prompt(char *buf, size_t len)
+{
+    char inp[2];
+    uint8_t idx = 0;
+    uint64_t time;
+
+    if (timeout) {
+        time = get_clock() + (timeout << 32);
+        set_clock_comparator(time);
+        enable_clock_int();
+    }
+
+    inp[1] = '\0';
+
+    while (!check_clock_int()) {
+
+        /* Process only one character at a time */
+        sclp_read(inp, 1);
+
+        switch (inp[0]) {
+        case KEYCODE_NO_INP:
+        case KEYCODE_ESCAPE:
+            continue;
+        case KEYCODE_BACKSP:
+            if (idx > 0) {
+                /* Remove last character */
+                buf[idx - 1] = ' ';
+                sclp_print("\r");
+                sclp_print(buf);
+
+                idx--;
+
+                /* Reset cursor */
+                buf[idx] = 0;
+                sclp_print("\r");
+                sclp_print(buf);
+            }
+            continue;
+        case KEYCODE_ENTER:
+            disable_clock_int();
+            return idx;
+        }
+
+        /* Echo input and add to buffer */
+        if (idx < len) {
+            buf[idx] = inp[0];
+            sclp_print(inp);
+            idx++;
+        }
+    }
+
+    disable_clock_int();
+    *buf = NULL;
+
+    return 0;
+}
+
+static int get_index(void)
+{
+    char buf[10];
+    int len;
+    int i;
+
+    memset(buf, 0, sizeof(buf));
+
+    len = read_prompt(buf, sizeof(buf));
+
+    if (len == 0) {
+        return 0;
+    }
+
+    for (i = 0; i < len; i++) {
Quoted 8 lines
Quoted 5 lines

+        if (!isdigit(buf[i])) {
+            return -1;
+        }
+    }
+

doesn't the atoi function already do the isdigit check? Is there a reason we are doing it here again?

c-like atoi ends the conversion when we encounter a non-numerical character. So 1234abc5 would return 1234.  It does not do any kind of error checking. The loop is to perform a more robust check to ensure we do not have any erroneous characters at all.

Thinking about it -- perhaps implementing a strtol-like function would be better (minus all the base-conversion stuff).  We can still use atoi for get_loadparm_index().

Quoted 47 lines
Quoted 47 lines

+    return atoi(buf);

pc-bios/s390-ccw/menu.c new file +45 -0
+}
+
+static int get_boot_index(int entries)
+{
+    char tmp[6];
+    int boot_index;
+
+    /* Prompt User */
+    if (timeout > 0) {
+        sclp_print("Please choose (default will boot in ");
+        sclp_print(itostr(timeout, tmp, sizeof(tmp)));
+        sclp_print(" seconds):\n");
+    } else {
+        sclp_print("Please choose:\n");
+    }
+
+    /* Get Menu Choice */
+    boot_index = get_index();
+
+    timeout = 0;
+
+    while (boot_index < 0 || boot_index >= entries) {
+        sclp_print("\nError: undefined configuration"
+                   "\nPlease choose:\n");
+        boot_index = get_index();
+    }
+
+    sclp_print("\nBooting entry #");
+    sclp_print(itostr(boot_index, tmp, sizeof(tmp)));
+
+    return boot_index;
+}
+
+static void zipl_println(const char *data, size_t len)
+{
+    char buf[len + 1];
+
+    ebcdic_to_ascii(data, buf, len);
+    buf[len] = '\n';
+    buf[len + 1] = '\0';
+
+    sclp_print(buf);
+}
+
+int menu_get_zipl_boot_index(const void *stage2, ZiplParms zipl_parms)
Quoted 8 lines

pc-bios/s390-ccw/menu.c line 191

Quoted 7 lines
pc-bios/s390-ccw/menu.c new file +7 -0
+{
+    const char *data = stage2 + zipl_parms.menu_start;
+    size_t len;
+    int ct;
+
+    if (flags & BOOT_MENU_FLAG_ZIPL_OPTS) {
+        if (zipl_parms.flag) {

should we not check for zipl_parms.timeout? or do we know that if flags is set than timeout will always be set?

If timeout was not specified in the zipl configuration file, then it will be 0. (which means wait forever)

Quoted 135 lines
Quoted 133 lines

+            timeout = zipl_parms.timeout;
+        } else {
+            return 0; /* Boot default */
+        }
+    }

pc-bios/s390-ccw/menu.c new file +27 -0
+
+    /* Print and count all menu items, including the banner */
+    for (ct = 0; *data; ct++) {
+        len = strlen(data);
+        zipl_println(data, len);
+        data += len + 1;
+
+        if (ct < 2) {
+            sclp_print("\n");
+        }
+    }
+
+    sclp_print("\n");
+
+    return get_boot_index(ct - 1);
+}
+
+void menu_set_parms(uint8_t boot_menu_flag, uint16_t boot_menu_timeout)
+{
+    flags = boot_menu_flag;
+    timeout = boot_menu_timeout;
+}
+
+int menu_check_flags(uint8_t check_flags)
+{
+    return flags & check_flags;
+}
pc-bios/s390-ccw/menu.h new file +7 -0
new file mode 100644
index 0000000..a8727fa
@@ -0,0 +1,28 @@
+/*
+ * QEMU S390 Interactive Boot Menu
+ *
+ * Copyright 2017 IBM Corp.
+ * Author: Collin L. Walling <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 

or (at
+ * your option) any later version. See the COPYING file in the top-level
+ * directory.
+ */

pc-bios/s390-ccw/menu.h new file +14 -0
+
+#ifndef MENU_H
+#define MENU_H
+
+#define BOOT_MENU_FLAG_BOOT_OPTS 0x80
+#define BOOT_MENU_FLAG_ZIPL_OPTS 0x40
+
+typedef struct ZiplParms {
+    unsigned short flag;
+    unsigned short timeout;
+    unsigned long long menu_start;
+} ZiplParms;
+
+void menu_set_parms(uint8_t boot_menu_flags, uint16_t 

boot_menu_timeout);

pc-bios/s390-ccw/menu.h new file +4 -0
+bool menu_check_flags(uint8_t check_flags);
+int menu_get_zipl_boot_index(const void *stage2, ZiplParms zipl_parms);
+
+#endif /* MENU_H */
pc-bios/s390-ccw/s390-ccw.h +2 -0
index 25d4d21..df4bc88 100644
@@ -71,6 +71,7 @@ unsigned int get_loadparm_index(void);
   void sclp_print(const char *string);
   void sclp_setup(void);
   void sclp_get_loadparm_ascii(char *loadparm);
+void sclp_read(char *str, size_t len);
 
   /* virtio.c */
   unsigned long virtio_load_direct(ulong rec_list1, ulong rec_list2,
@@ -79,6 +80,7 @@ bool virtio_is_supported(SubChannelId schid);
   void virtio_blk_setup_device(SubChannelId schid);
   int virtio_read(ulong sector, void *load_addr);
   int enable_mss_facility(void);
+u64 get_clock(void);
   ulong get_second(void);
 
   /* bootmap.c */
pc-bios/s390-ccw/sclp.c +20 -0
index 486fce1..5e4a78b 100644
@@ -101,3 +101,23 @@ void sclp_get_loadparm_ascii(char *loadparm)
           ebcdic_to_ascii((char *) sccb->loadparm, loadparm, 8);
       }
   }
+
+void sclp_read(char *str, size_t len)
+{
+    ReadEventData *sccb = (void *)_sccb;
+    char *buf = (char *)(&sccb->ebh) + 7;
+
+    /* Len should not exceed the maximum size of the event buffer */
+    if (len > SCCB_SIZE - 8) {
+        len = SCCB_SIZE - 8;
+    }
+
+    sccb->h.length = SCCB_SIZE;
+    sccb->h.function_code = SCLP_UNCONDITIONAL_READ;
+    sccb->ebh.length = sizeof(EventBufferHeader);
+    sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
+    sccb->ebh.flags = 0;
+
+    sclp_service_call(SCLP_CMD_READ_EVENT_DATA, sccb);
+    memcpy(str, buf, len);
+}
pc-bios/s390-ccw/virtio.c +1 -1
index c890a03..817e7f5 100644
@@ -176,7 +176,7 @@ void vring_send_buf(VRing *vr, void *p, int len, 
 int flags)
       }
   }
 
-static u64 get_clock(void)
+u64 get_clock(void)
   {
       u64 r;
Signature · 1 line
- Collin L Walling
#21 lore raw

Re: [Qemu-devel] [PATCH v2 4/5] s390-ccw: interactive boot menu for eckd dasd

In reply to Collin L. Walling
Quoted 31 lines · Collin L. Walling

When the boot menu options are present and the guest's disk has been configured by the zipl tool, then the user will be presented with an interactive boot menu with labeled entries. An example of what the menu might look like:

zIPL v1.37.1-build-20170714 interactive boot menu.

0. default (linux-4.13.0)

1. linux-4.13.0
2. performance
3. kvm

Please choose (default will boot in 10 seconds):

If the user's input is empty or 0, the default zipl entry will be chosen. If the input is within the range presented by the menu, then the selection will be booted. Any erroneous input will cancel the timeout and prompt the user until correct input is given.

Any value set for loadparm will override all boot menu options. If loadparm=PROMPT, then the menu prompt will continuously wait until correct user input is given.

The absence of any boot options on the command line will attempt to use the zipl loader values.

---

[...]

Quoted 30 lines
pc-bios/s390-ccw/bootmap.c +7 -1
index 5546b79..c817cf8 100644
@@ -13,6 +13,7 @@
 #include "bootmap.h"
 #include "virtio.h"
 #include "bswap.h"
+#include "menu.h"
 
 #ifdef DEBUG
 /* #define DEBUG_FALLBACK */
@@ -83,6 +84,7 @@ static void jump_to_IPL_code(uint64_t address)
 
 static unsigned char _bprs[8*1024]; /* guessed "max" ECKD sector size */
 static const int max_bprs_entries = sizeof(_bprs) / sizeof(ExtEckdBlockPtr);
+static uint8_t stage2[STAGE2_MAX_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
 
 static inline void verify_boot_info(BootInfo *bip)
 {
@@ -182,7 +184,57 @@ static block_number_t load_eckd_segments(block_number_t blk, uint64_t *address)
     return block_nr;
 }
 
-static void run_eckd_boot_script(block_number_t mbr_block_nr)
+static void read_stage2(block_number_t s1b_block_nr)
+{
+    block_number_t s2_block_nr;
+    EckdStage1b *s1b = (void *)sec;
+    int i;

pc-bios/s390-ccw/bootmap.c static block_number_t load_eckd_segments(block_number_t blk, uint64_t *address) · line 201

Quoted 10 lines
pc-bios/s390-ccw/bootmap.c +10 -0
+
+    /* Get Stage1b data */
+    memset(sec, FREE_SPACE_FILLER, sizeof(sec));
+    read_block(s1b_block_nr, s1b, "Cannot read stage1b boot loader.");
+
+    /* Get Stage2 data */
+    memset(stage2, FREE_SPACE_FILLER, sizeof(stage2));
+
+    for (i = 0; i < STAGE2_MAX_SIZE / MAX_SECTOR_SIZE; i++) {
+        s2_block_nr = eckd_block_num((void *)&(s1b->seek[i].cyl));

You can omit the parentheses around s1b->seek[i].cyl.

pc-bios/s390-ccw/bootmap.c static block_number_t load_eckd_segments(block_number_t blk, uint64_t *address) · line 207

Quoted 5 lines
pc-bios/s390-ccw/bootmap.c +5 -0
+        if (!s2_block_nr) {
+            break;
+        }
+
+        read_block(s2_block_nr, (stage2 + MAX_SECTOR_SIZE * i),

You can omit the parentheses around the second parameter.

Quoted 3 lines
pc-bios/s390-ccw/bootmap.c +3 -0
+                   "Error reading Stage2 data");
+    }
+}

[...]

pc-bios/s390-ccw/bootmap.c static void ipl_eckd_cdl(void) · line 304

Quoted 6 lines
pc-bios/s390-ccw/bootmap.c +2 -0
@@ -241,6 +300,9 @@ static void ipl_eckd_cdl(void)
     /* save pointer to Boot Script */
     mbr_block_nr = eckd_block_num((void *)&(mbr->blockptr));
 
+    /* save pointer to Stage1b Data */
+    s1b_block_nr = eckd_block_num((void *)&(ipl2->stage1.seek[0].cyl));

You can omit the parentheses around ipl2->stage1.seek[0].cyl.

Quoted 21 lines
pc-bios/s390-ccw/bootmap.c +2 -1
     memset(sec, FREE_SPACE_FILLER, sizeof(sec));
     read_block(2, vlbl, "Cannot read Volume Label at block 2");
     IPL_assert(magic_match(vlbl->key, VOL1_MAGIC),
@@ -249,7 +311,7 @@ static void ipl_eckd_cdl(void)
                "Invalid magic of volser block");
     print_volser(vlbl->f.volser);
 
-    run_eckd_boot_script(mbr_block_nr);
+    run_eckd_boot_script(mbr_block_nr, s1b_block_nr);
     /* no return */
 }
 
@@ -281,6 +343,7 @@ static void print_eckd_ldl_msg(ECKD_IPL_mode_t mode)
 static void ipl_eckd_ldl(ECKD_IPL_mode_t mode)
 {
     block_number_t mbr_block_nr;
+    block_number_t s1b_block_nr;
     EckdLdlIpl1 *ipl1 = (void *)sec;
 
     if (mode != ECKD_LDL_UNLABELED) {

pc-bios/s390-ccw/bootmap.c static void ipl_eckd_ldl(ECKD_IPL_mode_t mode) · line 368

Quoted 5 lines
pc-bios/s390-ccw/bootmap.c +1 -1
@@ -302,7 +365,9 @@ static void ipl_eckd_ldl(ECKD_IPL_mode_t mode)
     mbr_block_nr =
         eckd_block_num((void *)&(ipl1->boot_info.bp.ipl.bm_ptr.eckd.bptr));
 
-    run_eckd_boot_script(mbr_block_nr);
+    s1b_block_nr = eckd_block_num((void *)&(ipl1->stage1.seek[0].cyl));

dito.

Quoted 73 lines
pc-bios/s390-ccw/bootmap.c +1 -0
+    run_eckd_boot_script(mbr_block_nr, s1b_block_nr);
     /* no return */
 }
 
pc-bios/s390-ccw/bootmap.h +2 -0
index b700d08..8089402 100644
@@ -74,6 +74,7 @@ typedef struct ScsiMbr {
 } __attribute__ ((packed)) ScsiMbr;
 
 #define ZIPL_MAGIC              "zIPL"
+#define ZIPL_MAGIC_EBCDIC       "\xa9\xc9\xd7\xd3"
 #define IPL1_MAGIC "\xc9\xd7\xd3\xf1" /* == "IPL1" in EBCDIC */
 #define IPL2_MAGIC "\xc9\xd7\xd3\xf2" /* == "IPL2" in EBCDIC */
 #define VOL1_MAGIC "\xe5\xd6\xd3\xf1" /* == "VOL1" in EBCDIC */
@@ -229,6 +230,7 @@ typedef struct BootInfo {          /* @ 0x70, record #0    */
 /*
  * Structs for IPL
  */
+#define STAGE2_MAX_SIZE     0x3000
 #define STAGE2_BLK_CNT_MAX  24 /* Stage 1b can load up to 24 blocks */
 
 typedef struct EckdCdlIpl1 {
pc-bios/s390-ccw/main.c +3 -0
index a8ef120..fb0ef92 100644
@@ -11,6 +11,7 @@
 #include "libc.h"
 #include "s390-ccw.h"
 #include "virtio.h"
+#include "menu.h"
 
 char stack[PAGE_SIZE * 8] __attribute__((__aligned__(PAGE_SIZE)));
 static SubChannelId blk_schid = { .one = 1 };
@@ -101,6 +102,8 @@ static void virtio_setup(void)
             blk_schid.ssid = iplb.ccw.ssid & 0x3;
             debug_print_int("ssid ", blk_schid.ssid);
             found = find_dev(&schib, dev_no);
+            menu_set_parms(iplb.ccw.boot_menu_flags,
+                           iplb.ccw.boot_menu_timeout);
             break;
         case S390_IPL_TYPE_QEMU_SCSI:
             vdev->scsi_device_selected = true;
pc-bios/s390-ccw/menu.c new file +22 -0
new file mode 100644
index 0000000..d707afb
@@ -0,0 +1,223 @@
+/*
+ * QEMU S390 Interactive Boot Menu
+ *
+ * Copyright 2017 IBM Corp.
+ * Author: Collin L. Walling <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or (at
+ * your option) any later version. See the COPYING file in the top-level
+ * directory.
+ */
+
+#include "libc.h"
+#include "s390-ccw.h"
+#include "menu.h"
+
+#define KEYCODE_NO_INP '\0'
+#define KEYCODE_ESCAPE '\033'
+#define KEYCODE_BACKSP '\177'
+#define KEYCODE_ENTER  '\r'
+
+static uint8_t flags;
+static uint64_t timeout;

pc-bios/s390-ccw/menu.c line 32

Quoted 10 lines
pc-bios/s390-ccw/menu.c new file +10 -0
+
+static inline void enable_clock_int(void)
+{
+    uint64_t tmp = 0;
+
+    asm volatile(
+        "stctg      0,0,%0\n"
+        "oi         6+%0, 0x8\n"
+        "lctlg      0,0,%0"
+        : : "Q" (tmp)

Did you check whether we need "memory" in the clobber list here?

Quoted 15 lines
pc-bios/s390-ccw/menu.c new file +15 -0
+    );
+}
+
+static inline void disable_clock_int(void)
+{
+    uint64_t tmp = 0;
+
+    asm volatile(
+        "stctg      0,0,%0\n"
+        "ni         6+%0, 0xf7\n"
+        "lctlg      0,0,%0"
+        : : "Q" (tmp)
+    );
+}
+

pc-bios/s390-ccw/menu.c line 57

Quoted 10 lines
pc-bios/s390-ccw/menu.c new file +10 -0
+static inline void set_clock_comparator(uint64_t time)
+{
+    asm volatile("sckc %0" : : "Q" (time));
+}
+
+static inline bool check_clock_int(void)
+{
+    uint16_t code = *(uint16_t *)0x86;
+
+    consume_sclp_int();

Don't you rather have to read the code from memory *after* calling consume_sclp_int() ?

Quoted 6 lines
pc-bios/s390-ccw/menu.c new file +6 -0
+    return code == 0x1004;
+}
+
+static int read_prompt(char *buf, size_t len)
+{
+    char inp[2];

pc-bios/s390-ccw/menu.c line 74

Quoted 10 lines
pc-bios/s390-ccw/menu.c new file +10 -0
+    uint8_t idx = 0;
+    uint64_t time;
+
+    if (timeout) {
+        time = get_clock() + (timeout << 32);
+        set_clock_comparator(time);
+        enable_clock_int();
+    }
+
+    inp[1] = '\0';

I think I'd rather declare "char inp[2] = {}", just to make sure that inp[0] is also correctly pre-initialized - so that in case anything goes wrong with sclp_read() below, we can be sure that there is not random data in inp[0].

Quoted 29 lines
pc-bios/s390-ccw/menu.c new file +29 -0
+    while (!check_clock_int()) {
+
+        /* Process only one character at a time */
+        sclp_read(inp, 1);
+
+        switch (inp[0]) {
+        case KEYCODE_NO_INP:
+        case KEYCODE_ESCAPE:
+            continue;
+        case KEYCODE_BACKSP:
+            if (idx > 0) {
+                /* Remove last character */
+                buf[idx - 1] = ' ';
+                sclp_print("\r");
+                sclp_print(buf);
+
+                idx--;
+
+                /* Reset cursor */
+                buf[idx] = 0;
+                sclp_print("\r");
+                sclp_print(buf);
+            }
+            continue;
+        case KEYCODE_ENTER:
+            disable_clock_int();
+            return idx;
+        }
+

pc-bios/s390-ccw/menu.c line 114

Quoted 10 lines
pc-bios/s390-ccw/menu.c new file +10 -0
+        /* Echo input and add to buffer */
+        if (idx < len) {
+            buf[idx] = inp[0];
+            sclp_print(inp);
+            idx++;
+        }
+    }
+
+    disable_clock_int();
+    *buf = NULL;

Hmm, I'm used to see NULL only as a pointer, so "*buf = 0" would IMHO be nicer here?

pc-bios/s390-ccw/menu.c line 127

Quoted 13 lines
pc-bios/s390-ccw/menu.c new file +13 -0
+
+    return 0;
+}
+
+static int get_index(void)
+{
+    char buf[10];
+    int len;
+    int i;
+
+    memset(buf, 0, sizeof(buf));
+
+    len = read_prompt(buf, sizeof(buf));

I think you should rather use "sizeof(buf) - 1" here to make sure that the string is always terminated with a 0?

pc-bios/s390-ccw/menu.c line 140

Quoted 12 lines
pc-bios/s390-ccw/menu.c new file +12 -0
+    if (len == 0) {
+        return 0;
+    }
+
+    for (i = 0; i < len; i++) {
+        if (!isdigit(buf[i])) {
+            return -1;
+        }
+    }
+
+    return atoi(buf);
+}

Thomas

#22 lore raw

PATCH v2 5/5 s390-ccw: interactive boot menu for scsi

In reply to Collin L. Walling

Interactive boot menu for scsi. This follows the same procedure as the interactive menu for eckd dasd. An example follows:

s390x Enumerated Boot Menu.

3 entries detected. Select from index 0 to 2.

Please choose:

---
 pc-bios/s390-ccw/bootmap.c |  9 ++++++---
 pc-bios/s390-ccw/main.c    |  2 ++
 pc-bios/s390-ccw/menu.c    | 14 ++++++++++++++
 pc-bios/s390-ccw/menu.h    |  1 +
 4 files changed, 23 insertions(+), 3 deletions(-)
pc-bios/s390-ccw/bootmap.c +6 -3
index c817cf8..78e41ab 100644
@@ -552,15 +552,18 @@ static void ipl_scsi(void)
         }
 
         program_table_entries++;
-        if (program_table_entries == loadparm + 1) {
-            break; /* selected entry found */
-        }
     }
 
     debug_print_int("program table entries", program_table_entries);
 
     IPL_assert(program_table_entries != 0, "Empty Program Table");
 
+    if (menu_check_flags(BOOT_MENU_FLAG_BOOT_OPTS)) {
+        loadparm = menu_get_enum_boot_index(program_table_entries);
+    }
+
+    prog_table_entry = (ScsiBlockPtr *)(sec + pte_len * (loadparm + 1));
+
     zipl_run(prog_table_entry); /* no return */
 }
 
pc-bios/s390-ccw/main.c +2 -0
index fb0ef92..2a697a0 100644
@@ -112,6 +112,8 @@ static void virtio_setup(void)
             vdev->selected_scsi_device.lun = iplb.scsi.lun;
             blk_schid.ssid = iplb.scsi.ssid & 0x3;
             found = find_dev(&schib, iplb.scsi.devno);
+            menu_set_parms(iplb.scsi.boot_menu_flags,
+                            iplb.scsi.boot_menu_timeout);
             break;
         default:
             panic("List-directed IPL not supported yet!\n");
pc-bios/s390-ccw/menu.c +14 -0
index d707afb..49876c1 100644
@@ -211,6 +211,20 @@ int menu_get_zipl_boot_index(const void *stage2, ZiplParms zipl_parms)
     return get_boot_index(ct - 1);
 }
 
+int menu_get_enum_boot_index(int entries)
+{
+    char tmp[4];
+
+    sclp_print("s390x Enumerated Boot Menu.\n\n");
+
+    sclp_print(itostr(entries, tmp, sizeof(tmp)));
+    sclp_print(" entries detected. Select from boot index 0 to ");
+    sclp_print(itostr(entries - 1, tmp, sizeof(tmp)));
+    sclp_print(".\n\n");
+
+    return get_boot_index(entries);
+}
+
 void menu_set_parms(uint8_t boot_menu_flag, uint16_t boot_menu_timeout)
 {
     flags = boot_menu_flag;
pc-bios/s390-ccw/menu.h +1 -0
index a8727fa..4373b0c 100644
@@ -24,5 +24,6 @@ typedef struct ZiplParms {
 void menu_set_parms(uint8_t boot_menu_flags, uint16_t boot_menu_timeout);
 bool menu_check_flags(uint8_t check_flags);
 int menu_get_zipl_boot_index(const void *stage2, ZiplParms zipl_parms);
+int menu_get_enum_boot_index(int entries);
 
 #endif /* MENU_H */
Signature · 1 line
2.7.4