1 /* 2 * Copyright (c) 2018-present Facebook. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <ipmid/api.h> 18 #include <stdbool.h> 19 #include <stdio.h> 20 #include <sys/stat.h> 21 22 #include <appcommands.hpp> 23 #include <ipmid/api.hpp> 24 #include <ipmid/utils.hpp> 25 #include <nlohmann/json.hpp> 26 #include <phosphor-logging/lg2.hpp> 27 #include <phosphor-logging/log.hpp> 28 #include <sdbusplus/asio/connection.hpp> 29 #include <sdbusplus/asio/property.hpp> 30 31 #include <fstream> 32 #include <iomanip> 33 #include <iostream> 34 #include <sstream> 35 36 namespace ipmi 37 { 38 39 static constexpr bool DEBUG = false; 40 41 static constexpr auto JSON_POST_DATA_FILE = 42 "/usr/share/lcd-debug/post_desc.json"; 43 static constexpr auto JSON_GPIO_DATA_FILE = 44 "/usr/share/lcd-debug/gpio_desc.json"; 45 static constexpr auto JSON_SENSOR_NAMES_FILE = 46 "/usr/share/lcd-debug/cri_sensors.json"; 47 48 static constexpr auto ETH_INTF_NAME = "eth0"; 49 50 #define ESCAPE "\x1B" 51 #define ESC_BAT ESCAPE "B" 52 #define ESC_MCU_BL_VER ESCAPE "U" 53 #define ESC_MCU_RUN_VER ESCAPE "R" 54 #define ESC_ALT ESCAPE "[5;7m" 55 #define ESC_RST ESCAPE "[m" 56 57 static constexpr char LINE_DELIMITER = '\x1F'; 58 59 static constexpr size_t FRAME_BUFF_SIZE = 4096; 60 static constexpr size_t FRAME_PAGE_BUF_SIZE = 256; 61 62 #define FRU_ALL 0 63 64 static constexpr auto DEBUG_GPIO_KEY = "GpioDesc"; 65 static constexpr auto GPIO_ARRAY_SIZE = 4; 66 static constexpr auto GPIO_PIN_INDEX = 0; 67 static constexpr auto GPIO_LEVEL_INDEX = 1; 68 static constexpr auto GPIO_DEF_INDEX = 2; 69 static constexpr auto GPIO_DESC_INDEX = 3; 70 71 static constexpr size_t BMC_POSITION = 0; 72 73 static constexpr uint8_t meAddress = 1; 74 static constexpr uint8_t lun = 0; 75 76 using IpmbMethodType = 77 std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>; 78 79 struct frame 80 { 81 char title[32]; 82 size_t max_size; 83 size_t max_page; 84 char* buf; 85 size_t idx_head, idx_tail; 86 size_t line_per_page; 87 size_t line_width; 88 size_t lines, pages; 89 uint8_t esc_sts; 90 bool overwrite; 91 time_t mtime; 92 frameipmi::frame93 frame() : buf(nullptr), pages(0), mtime(0) {} 94 95 void init(size_t size = FRAME_BUFF_SIZE); 96 void append(const std::string& str, size_t indent = 0); 97 int getPage(size_t page, char* page_buf, size_t page_buf_size); 98 bool isFull() const; 99 bool isEscSeq(char chr); 100 101 private: 102 auto parse(const std::string& input, size_t indent) -> std::string; 103 }; 104 105 frame frame_info; 106 frame frame_sel; 107 frame frame_snr; 108 frame frame_postcode; 109 110 enum class panel : uint8_t 111 { 112 NONE = 0, 113 MAIN = 1, 114 BOOT_ORDER = 2, 115 POWER_POLICY = 3, 116 }; 117 118 struct ctrl_panel 119 { 120 panel parent; 121 size_t item_num; 122 std::array<std::string, 8> item_str; 123 panel (*select)(size_t item); 124 }; 125 126 static panel panel_main(size_t item); 127 static panel panel_boot_order(size_t item); 128 static panel panel_power_policy(size_t item); 129 130 static ctrl_panel panels[] = { 131 {/* dummy entry for making other to 1-based */}, 132 { 133 .parent = panel::MAIN, 134 .item_num = 2, 135 .item_str = 136 { 137 "User Setting", 138 ">Boot Order", 139 ">Power Policy", 140 }, 141 .select = panel_main, 142 }, 143 { 144 .parent = panel::MAIN, 145 .item_num = 0, 146 .item_str = 147 { 148 "Boot Order", 149 }, 150 .select = panel_boot_order, 151 }, 152 { 153 .parent = panel::MAIN, 154 .item_num = 0, 155 .item_str = 156 { 157 "Power Policy", 158 }, 159 .select = panel_power_policy, 160 }, 161 }; 162 163 } // end of namespace ipmi 164