| 1 | /* |
| 2 | * s390 IPL device |
| 3 | * |
| 4 | * Copyright 2015, 2020 IBM Corp. |
| 5 | * Author(s): Zhang Fan <bjfanzh@cn.ibm.com> |
| 6 | * Janosch Frank <frankja@linux.ibm.com> |
| 7 | * |
| 8 | * This work is licensed under the terms of the GNU GPL, version 2 or (at |
| 9 | * your option) any later version. See the COPYING file in the top-level |
| 10 | * directory. |
| 11 | */ |
| 12 | |
| 13 | #ifndef HW_S390_IPL_H |
| 14 | #define HW_S390_IPL_H |
| 15 | |
| 16 | #include "cert-store.h" |
| 17 | #include "target/s390x/cpu.h" |
| 18 | #include "exec/target_page.h" |
| 19 | #include "system/address-spaces.h" |
| 20 | #include "system/memory.h" |
| 21 | #include "hw/core/qdev.h" |
| 22 | #include "hw/s390x/ipl/qipl.h" |
| 23 | #include "qom/object.h" |
| 24 | #include "target/s390x/kvm/pv.h" |
| 25 | |
| 26 | void s390_ipl_convert_loadparm(char *ascii_lp, uint8_t *ebcdic_lp); |
| 27 | void s390_ipl_fmt_loadparm(uint8_t *loadparm, char *str, Error **errp); |
| 28 | void s390_rebuild_iplb(uint16_t index, IplParameterBlock *iplb); |
| 29 | void s390_ipl_update_diag308(IplParameterBlock *iplb); |
| 30 | int s390_ipl_prepare_pv_header(struct S390PVResponse *pv_resp, |
| 31 | Error **errp); |
| 32 | int s390_ipl_pv_unpack(struct S390PVResponse *pv_resp); |
| 33 | void s390_ipl_prepare_cpu(S390CPU *cpu); |
| 34 | IplParameterBlock *s390_ipl_get_iplb(void); |
| 35 | IplParameterBlock *s390_ipl_get_iplb_pv(void); |
| 36 | S390IPLCertificateStore *s390_ipl_get_certificate_store(void); |
| 37 | |
| 38 | enum s390_reset { |
| 39 | /* default is a reset not triggered by a CPU e.g. issued by QMP */ |
| 40 | S390_RESET_EXTERNAL = 0, |
| 41 | S390_RESET_REIPL, |
| 42 | S390_RESET_MODIFIED_CLEAR, |
| 43 | S390_RESET_LOAD_NORMAL, |
| 44 | S390_RESET_PV, |
| 45 | }; |
| 46 | void s390_ipl_reset_request(CPUState *cs, enum s390_reset reset_type); |
| 47 | void s390_ipl_get_reset_request(CPUState **cs, enum s390_reset *reset_type); |
| 48 | void s390_ipl_clear_reset_request(void); |
| 49 | |
| 50 | #define QIPL_ADDRESS 0xcc |
| 51 | |
| 52 | /* Boot Menu flags */ |
| 53 | #define QIPL_FLAG_BM_OPTS_CMD 0x80 |
| 54 | #define QIPL_FLAG_BM_OPTS_ZIPL 0x40 |
| 55 | |
| 56 | #define TYPE_S390_IPL "s390-ipl" |
| 57 | OBJECT_DECLARE_SIMPLE_TYPE(S390IPLState, S390_IPL) |
| 58 | |
| 59 | struct S390IPLState { |
| 60 | /*< private >*/ |
| 61 | DeviceState parent_obj; |
| 62 | IplParameterBlock iplb; |
| 63 | IplParameterBlock iplb_pv; |
| 64 | QemuIplParameters qipl; |
| 65 | S390IPLCertificateStore cert_store; |
| 66 | uint64_t start_addr; |
| 67 | uint64_t compat_start_addr; |
| 68 | uint64_t bios_start_addr; |
| 69 | uint64_t compat_bios_start_addr; |
| 70 | bool enforce_bios; |
| 71 | bool iplb_valid; |
| 72 | bool iplb_valid_pv; |
| 73 | bool rebuilt_iplb; |
| 74 | uint16_t iplb_index; |
| 75 | /* reset related properties don't have to be migrated or reset */ |
| 76 | enum s390_reset reset_type; |
| 77 | int reset_cpu_index; |
| 78 | |
| 79 | /*< public >*/ |
| 80 | char *kernel; |
| 81 | char *initrd; |
| 82 | char *cmdline; |
| 83 | char *firmware; |
| 84 | uint8_t cssid; |
| 85 | uint8_t ssid; |
| 86 | uint16_t devno; |
| 87 | }; |
| 88 | QEMU_BUILD_BUG_MSG(offsetof(S390IPLState, iplb) & 3, "alignment of iplb wrong"); |
| 89 | |
| 90 | static inline bool iplb_valid_len(IplParameterBlock *iplb) |
| 91 | { |
| 92 | return be32_to_cpu(iplb->len) <= sizeof(IplParameterBlock); |
| 93 | } |
| 94 | |
| 95 | static inline bool ipl_valid_pv_components(IplParameterBlock *iplb) |
| 96 | { |
| 97 | IPLBlockPV *ipib_pv = &iplb->pv; |
| 98 | int i; |
| 99 | |
| 100 | if (ipib_pv->num_comp == 0) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | if (offsetof(IplParameterBlock, pv.components) + |
| 105 | ipib_pv->num_comp * sizeof(IPLBlockPVComp) > |
| 106 | be32_to_cpu(iplb->len)) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | for (i = 0; i < ipib_pv->num_comp; i++) { |
| 111 | /* Addr must be 4k aligned */ |
| 112 | if (ipib_pv->components[i].addr & ~TARGET_PAGE_MASK) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | /* Tweak prefix is monotonically increasing with each component */ |
| 117 | if (i < ipib_pv->num_comp - 1 && |
| 118 | ipib_pv->components[i].tweak_pref >= |
| 119 | ipib_pv->components[i + 1].tweak_pref) { |
| 120 | return false; |
| 121 | } |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | static inline bool ipl_valid_pv_header(IplParameterBlock *iplb) |
| 127 | { |
| 128 | IPLBlockPV *ipib_pv = &iplb->pv; |
| 129 | |
| 130 | if (ipib_pv->pv_header_len > 2 * TARGET_PAGE_SIZE) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | if (!address_space_access_valid(&address_space_memory, |
| 135 | ipib_pv->pv_header_addr, |
| 136 | ipib_pv->pv_header_len, |
| 137 | false, |
| 138 | MEMTXATTRS_UNSPECIFIED)) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | static inline bool iplb_valid_pv(IplParameterBlock *iplb) |
| 146 | { |
| 147 | if (iplb->pbt != S390_IPL_TYPE_PV || |
| 148 | be32_to_cpu(iplb->len) < S390_IPLB_MIN_PV_LEN) { |
| 149 | return false; |
| 150 | } |
| 151 | if (!ipl_valid_pv_header(iplb)) { |
| 152 | return false; |
| 153 | } |
| 154 | return ipl_valid_pv_components(iplb); |
| 155 | } |
| 156 | |
| 157 | static inline bool iplb_valid(IplParameterBlock *iplb) |
| 158 | { |
| 159 | uint32_t len = be32_to_cpu(iplb->len); |
| 160 | |
| 161 | switch (iplb->pbt) { |
| 162 | case S390_IPL_TYPE_FCP: |
| 163 | return len >= S390_IPLB_MIN_FCP_LEN; |
| 164 | case S390_IPL_TYPE_CCW: |
| 165 | return len >= S390_IPLB_MIN_CCW_LEN; |
| 166 | case S390_IPL_TYPE_PCI: |
| 167 | return len >= S390_IPLB_MIN_PCI_LEN; |
| 168 | case S390_IPL_TYPE_QEMU_SCSI: |
| 169 | default: |
| 170 | return false; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | #endif |