xref: /openbmc/fb-ipmi-oem/src/usb-dbg.cpp (revision 2f45499fc3a71ae67774061e95a5b3c6febf1893)
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 <commandutils.hpp>
18 #include <usb-dbg.hpp>
19 
20 namespace ipmi
21 {
22 
23 ipmi_ret_t getNetworkData(uint8_t lan_param, char* data);
24 std::string getMotherBoardFruName();
25 int8_t getFruData(std::string& serial, std::string& name);
26 int8_t sysConfig(std::vector<std::string>& data, size_t pos);
27 int8_t procInfo(std::string& result, size_t pos);
28 
29 bool isMultiHostPlatform();
30 
31 /* Declare Host Selector interface and path */
32 namespace selector
33 {
34 const std::string path = "/xyz/openbmc_project/Chassis/Buttons/HostSelector";
35 const std::string interface =
36     "xyz.openbmc_project.Chassis.Buttons.HostSelector";
37 } // namespace selector
38 
39 /* Declare storage functions used here */
40 namespace storage
41 {
42 int getSensorValue(std::string&, double&);
43 int getSensorUnit(std::string&, std::string&);
44 int getSensorThreshold(std::string&, std::string&);
45 } // namespace storage
46 
47 namespace boot
48 {
49 std::tuple<std::string, std::string> objPath(size_t id);
50 void setBootOrder(std::string bootObjPath, const std::vector<uint8_t>& bootSeq,
51                   std::string hostName);
52 void getBootOrder(std::string bootObjPath, std::vector<uint8_t>& bootSeq,
53                   std::string hostName);
54 } // namespace boot
55 
getMaxHostPosition(size_t & maxPosition)56 void getMaxHostPosition(size_t& maxPosition)
57 {
58     try
59     {
60         std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
61         std::string service =
62             getService(*dbus, ipmi::selector::interface, ipmi::selector::path);
63         Value variant =
64             getDbusProperty(*dbus, service, ipmi::selector::path,
65                             ipmi::selector::interface, "MaxPosition");
66         maxPosition = std::get<size_t>(variant);
67     }
68     catch (const std::exception& e)
69     {
70         lg2::error("Unable to get max host position - {MAXPOSITION}",
71                    "MAXPOSITION", maxPosition);
72         throw e;
73     }
74 }
75 
getSelectorPosition(size_t & hostPosition)76 void getSelectorPosition(size_t& hostPosition)
77 {
78     try
79     {
80         std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
81         std::string service =
82             getService(*dbus, ipmi::selector::interface, ipmi::selector::path);
83         Value variant = getDbusProperty(*dbus, service, ipmi::selector::path,
84                                         ipmi::selector::interface, "Position");
85         hostPosition = std::get<size_t>(variant);
86     }
87     catch (const std::exception& e)
88     {
89         lg2::error("Unable to get host position - {POSITION}", "POSITION",
90                    hostPosition);
91         throw e;
92     }
93 }
94 
95 static int panelNum = (sizeof(panels) / sizeof(struct ctrl_panel)) - 1;
96 
97 /* Returns the FRU the hand-switch is switched to. If it is switched to BMC
98  * it returns FRU_ALL. Note, if in err, it returns FRU_ALL */
plat_get_fru_sel()99 static size_t plat_get_fru_sel()
100 {
101     size_t position;
102     bool platform = isMultiHostPlatform();
103     if (platform == true)
104     {
105         getSelectorPosition(position);
106         if (position == BMC_POSITION)
107         {
108             return FRU_ALL;
109         }
110     }
111     else
112     {
113         /* For Tiogapass it just return 1,
114          *  can modify to support more platform */
115         position = 1;
116     }
117     return position;
118 }
119 
120 // return 0 on seccuess
init(size_t size)121 void frame::init(size_t size)
122 {
123     // Reset status
124     idx_head = idx_tail = 0;
125     lines = 0;
126     esc_sts = 0;
127     pages = 1;
128 
129     if (buf != nullptr && max_size == size)
130     {
131         return;
132     }
133 
134     if (buf != nullptr && max_size != size)
135     {
136         delete[] buf;
137     }
138     // Initialize Configuration
139     title[0] = '\0';
140     buf = new char[size];
141     max_size = size;
142     max_page = size;
143     line_per_page = 7;
144     line_width = 16;
145     overwrite = false;
146 
147     return;
148 }
149 
150 // return 0 on seccuess
append(const std::string & str,size_t indent)151 void frame::append(const std::string& str, size_t indent)
152 {
153     for (auto ch : parse(str, indent))
154     {
155         if (isFull())
156         {
157             if (overwrite)
158             {
159                 if (buf[idx_head] == LINE_DELIMITER)
160                     lines--;
161                 idx_head = (idx_head + 1) % max_size;
162             }
163             else
164             {
165                 throw std::overflow_error("No room in buffer");
166             }
167         }
168 
169         buf[idx_tail] = ch;
170         if (ch == LINE_DELIMITER)
171             lines++;
172 
173         idx_tail = (idx_tail + 1) % max_size;
174     }
175 
176     pages = (lines / line_per_page) + ((lines % line_per_page) ? 1 : 0);
177 
178     if (pages > max_page)
179         pages = max_page;
180 
181     return;
182 }
183 
184 // return page size
getPage(size_t page,char * page_buf,size_t page_buf_size)185 int frame::getPage(size_t page, char* page_buf, size_t page_buf_size)
186 {
187     int ret;
188     uint16_t line = 0;
189     uint16_t idx, len;
190 
191     if (buf == nullptr)
192         return -1;
193 
194     // 1-based page
195     if (page > pages || page < 1)
196         return -1;
197 
198     if (page_buf == nullptr || page_buf_size == 0)
199         return -1;
200 
201     ret = snprintf(page_buf, 17, "%-10s %02zd/%02zd", title, page, pages);
202     len = strlen(page_buf);
203     if (ret < 0)
204         return -1;
205 
206     line = 0;
207     idx = idx_head;
208     while (line < ((page - 1) * line_per_page) && idx != idx_tail)
209     {
210         if (buf[idx] == LINE_DELIMITER)
211             line++;
212         idx = (idx + 1) % max_size;
213     }
214 
215     while (line < ((page)*line_per_page) && idx != idx_tail)
216     {
217         if (buf[idx] == LINE_DELIMITER)
218         {
219             line++;
220         }
221         else
222         {
223             page_buf[len++] = buf[idx];
224             if (len == (page_buf_size - 1))
225             {
226                 break;
227             }
228         }
229         idx = (idx + 1) % max_size;
230     }
231 
232     return len;
233 }
234 
isFull() const235 bool frame::isFull() const
236 {
237     if (buf == nullptr)
238         return true;
239 
240     if ((idx_tail + 1) % max_size == idx_head)
241         return true;
242     else
243         return false;
244 }
245 
246 // return 1 for Escape Sequence
isEscSeq(char chr)247 bool frame::isEscSeq(char chr)
248 {
249     uint8_t curr_sts = esc_sts;
250 
251     if (esc_sts == 0 && (chr == 0x1b))
252         esc_sts = 1; // Escape Sequence
253     else if (esc_sts == 1 && (chr == 0x5b))
254         esc_sts = 2; // Control Sequence Introducer(CSI)
255     else if (esc_sts == 1 && (chr != 0x5b))
256         esc_sts = 0;
257     else if (esc_sts == 2 && (chr >= 0x40 && chr <= 0x7e))
258         esc_sts = 0;
259 
260     if (curr_sts || esc_sts)
261         return true;
262     else
263         return false;
264 }
265 
266 // return 0 on success
parse(const std::string & input,size_t indent)267 auto frame::parse(const std::string& input, size_t indent) -> std::string
268 {
269     if (indent > line_width)
270         return {};
271 
272     std::string result;
273     size_t linepos = 0;
274 
275     for (auto ch : input)
276     {
277         if (linepos == 0)
278         {
279             result.append(indent, ' ');
280             linepos = indent;
281         }
282 
283         // Insert character.
284         result.push_back(ch);
285 
286         if (!isEscSeq(ch))
287         {
288             // Check if new line is needed.
289             if (++linepos == line_width)
290             {
291                 result.push_back(LINE_DELIMITER);
292                 linepos = 0;
293             }
294         }
295     }
296 
297     // Fill out remaining line.
298     result.append(line_width - linepos, ' ');
299     result.push_back(LINE_DELIMITER);
300 
301     return result;
302 }
303 
chk_cri_sel_update(uint8_t * cri_sel_up)304 static int chk_cri_sel_update(uint8_t* cri_sel_up)
305 {
306     FILE* fp;
307     struct stat file_stat;
308     size_t pos = plat_get_fru_sel();
309     static uint8_t pre_pos = 0xff;
310 
311     fp = fopen("/mnt/data/cri_sel", "r");
312     if (fp)
313     {
314         if ((stat("/mnt/data/cri_sel", &file_stat) == 0) &&
315             (file_stat.st_mtime != frame_sel.mtime || pre_pos != pos))
316         {
317             *cri_sel_up = 1;
318         }
319         else
320         {
321             *cri_sel_up = 0;
322         }
323         fclose(fp);
324     }
325     else
326     {
327         if (frame_sel.buf == nullptr || frame_sel.lines != 0 || pre_pos != pos)
328         {
329             *cri_sel_up = 1;
330         }
331         else
332         {
333             *cri_sel_up = 0;
334         }
335     }
336     pre_pos = pos;
337     return 0;
338 }
339 
plat_udbg_get_frame_info(uint8_t * num)340 int plat_udbg_get_frame_info(uint8_t* num)
341 {
342     *num = 3;
343     return 0;
344 }
345 
plat_udbg_get_updated_frames(uint8_t * count,uint8_t * buffer)346 int plat_udbg_get_updated_frames(uint8_t* count, uint8_t* buffer)
347 {
348     uint8_t cri_sel_up = 0;
349     uint8_t info_page_up = 1;
350 
351     *count = 0;
352 
353     // info page update
354     if (info_page_up == 1)
355     {
356         buffer[*count] = 1;
357         *count += 1;
358     }
359 
360     // cri sel update
361     chk_cri_sel_update(&cri_sel_up);
362     if (cri_sel_up == 1)
363     {
364         buffer[*count] = 2;
365         *count += 1;
366     }
367 
368     // cri sensor update
369     buffer[*count] = 3;
370     *count += 1;
371 
372     return 0;
373 }
374 
plat_udbg_get_post_desc(uint8_t index,uint8_t * next,uint8_t phase,uint8_t * end,uint8_t * length,uint8_t * buffer)375 int plat_udbg_get_post_desc(uint8_t index, uint8_t* next, uint8_t phase,
376                             uint8_t* end, uint8_t* length, uint8_t* buffer)
377 {
378     nlohmann::json postObj;
379     std::string postCode;
380 
381     /* Get post description data stored in json file */
382     std::ifstream file(JSON_POST_DATA_FILE);
383     if (file)
384     {
385         file >> postObj;
386         file.close();
387     }
388     else
389     {
390         phosphor::logging::log<phosphor::logging::level::ERR>(
391             "Post code description file not found",
392             phosphor::logging::entry("POST_CODE_FILE=%s", JSON_POST_DATA_FILE));
393         return -1;
394     }
395 
396     std::string phaseStr = "PhaseAny";
397     if (postObj.find(phaseStr) == postObj.end())
398     {
399         phaseStr = "Phase" + std::to_string(phase);
400     }
401 
402     if (postObj.find(phaseStr) == postObj.end())
403     {
404         phosphor::logging::log<phosphor::logging::level::ERR>(
405             "Post code phase not available",
406             phosphor::logging::entry("PHASE=%d", phase));
407         return -1;
408     }
409 
410     auto phaseObj = postObj[phaseStr];
411     int phaseSize = phaseObj.size();
412 
413     for (int i = 0; i < phaseSize; i++)
414     {
415         postCode = phaseObj[i][0];
416         if (index == stoul(postCode, nullptr, 16))
417         {
418             std::string postDesc = phaseObj[i][1];
419             *length = postDesc.size();
420             memcpy(buffer, postDesc.data(), *length);
421             buffer[*length] = '\0';
422 
423             if (phaseSize != i + 1)
424             {
425                 postCode = phaseObj[i + 1][0];
426                 *next = stoul(postCode, nullptr, 16);
427                 *end = 0;
428             }
429             else
430             {
431                 if (postObj.size() != phase)
432                 {
433                     std::string nextPhaseStr =
434                         "Phase" + std::to_string(phase + 1);
435                     postCode = postObj[nextPhaseStr][0][0];
436                     *next = stoul(postCode, nullptr, 16);
437                     *end = 0;
438                 }
439                 else
440                 {
441                     *next = 0xff;
442                     *end = 1;
443                 }
444             }
445 
446             return 0;
447         }
448     }
449 
450     phosphor::logging::log<phosphor::logging::level::ERR>(
451         "Post code description data not available",
452         phosphor::logging::entry("PHASE_CODE=%d_0x%x", phase, index));
453     return -1;
454 }
455 
plat_udbg_get_gpio_desc(uint8_t index,uint8_t * next,uint8_t * level,uint8_t * def,uint8_t * length,uint8_t * buffer)456 int plat_udbg_get_gpio_desc(uint8_t index, uint8_t* next, uint8_t* level,
457                             uint8_t* def, uint8_t* length, uint8_t* buffer)
458 {
459     nlohmann::json gpioObj;
460     std::string gpioPin;
461 
462     /* Get gpio data stored in json file */
463     std::ifstream file(JSON_GPIO_DATA_FILE);
464     if (file)
465     {
466         file >> gpioObj;
467         file.close();
468     }
469     else
470     {
471         phosphor::logging::log<phosphor::logging::level::ERR>(
472             "GPIO pin description file not found",
473             phosphor::logging::entry("GPIO_PIN_DETAILS_FILE=%s",
474                                      JSON_GPIO_DATA_FILE));
475         return -1;
476     }
477 
478     if (gpioObj.find(DEBUG_GPIO_KEY) == gpioObj.end())
479     {
480         phosphor::logging::log<phosphor::logging::level::ERR>(
481             "GPIO pin details not available",
482             phosphor::logging::entry("GPIO_JSON_KEY=%d", DEBUG_GPIO_KEY));
483         return -1;
484     }
485 
486     auto obj = gpioObj[DEBUG_GPIO_KEY];
487     int objSize = obj.size();
488 
489     for (int i = 0; i < objSize; i++)
490     {
491         if (obj[i].size() != GPIO_ARRAY_SIZE)
492         {
493             phosphor::logging::log<phosphor::logging::level::ERR>(
494                 "Size of gpio array is incorrect",
495                 phosphor::logging::entry("EXPECTED_SIZE=%d", GPIO_ARRAY_SIZE));
496             return -1;
497         }
498 
499         gpioPin = obj[i][GPIO_PIN_INDEX];
500         if (index == stoul(gpioPin, nullptr, 16))
501         {
502             if (objSize != i + 1)
503             {
504                 gpioPin = obj[i + 1][GPIO_PIN_INDEX];
505                 *next = stoul(gpioPin, nullptr, 16);
506             }
507             else
508             {
509                 *next = 0xff;
510             }
511 
512             *level = obj[i][GPIO_LEVEL_INDEX];
513             *def = obj[i][GPIO_DEF_INDEX];
514             std::string gpioDesc = obj[i][GPIO_DESC_INDEX];
515             *length = gpioDesc.size();
516             memcpy(buffer, gpioDesc.data(), *length);
517             buffer[*length] = '\0';
518 
519             return 0;
520         }
521     }
522 
523     phosphor::logging::log<phosphor::logging::level::ERR>(
524         "GPIO pin description data not available",
525         phosphor::logging::entry("GPIO_PIN=0x%x", index));
526     return -1;
527 }
528 
getBiosVer(std::string & ver,size_t hostPosition)529 static int getBiosVer(std::string& ver, size_t hostPosition)
530 {
531     std::string sysfwVersionFile = std::format(SYSFW_VER_FILE, hostPosition);
532     std::ifstream file(sysfwVersionFile);
533     if (!file)
534     {
535         phosphor::logging::log<phosphor::logging::level::ERR>(
536             "Failed to open system firmware version file",
537             phosphor::logging::entry("FILE=%s", sysfwVersionFile.c_str()));
538         return -1;
539     }
540 
541     std::getline(file, ver);
542     file.close();
543 
544     return 0;
545 }
546 
sendBicCmd(uint8_t netFn,uint8_t cmd,uint8_t bicAddr,std::vector<uint8_t> & cmdData,std::vector<uint8_t> & respData)547 int sendBicCmd(uint8_t netFn, uint8_t cmd, uint8_t bicAddr,
548                std::vector<uint8_t>& cmdData, std::vector<uint8_t>& respData)
549 {
550     static constexpr uint8_t lun = 0;
551 
552     auto bus = getSdBus();
553 
554     auto method = bus->new_method_call("xyz.openbmc_project.Ipmi.Channel.Ipmb",
555                                        "/xyz/openbmc_project/Ipmi/Channel/Ipmb",
556                                        "org.openbmc.Ipmb", "sendRequest");
557     method.append(bicAddr, netFn, lun, cmd, cmdData);
558 
559     auto reply = bus->call(method);
560     if (reply.is_method_error())
561     {
562         phosphor::logging::log<phosphor::logging::level::ERR>(
563             "Error reading from BIC");
564         return -1;
565     }
566 
567     IpmbMethodType resp;
568     reply.read(resp);
569 
570     respData =
571         std::move(std::get<std::remove_reference_t<decltype(respData)>>(resp));
572 
573     return 0;
574 }
575 
sendMeCmd(uint8_t netFn,uint8_t cmd,std::vector<uint8_t> & cmdData,std::vector<uint8_t> & respData)576 int sendMeCmd(uint8_t netFn, uint8_t cmd, std::vector<uint8_t>& cmdData,
577               std::vector<uint8_t>& respData)
578 {
579     auto bus = getSdBus();
580 
581     if (DEBUG)
582     {
583         std::cout << "ME NetFn:cmd " << (int)netFn << ":" << (int)cmd << "\n";
584         std::cout << "ME req data: ";
585         for (auto d : cmdData)
586         {
587             std::cout << d << " ";
588         }
589         std::cout << "\n";
590     }
591 
592     auto method = bus->new_method_call("xyz.openbmc_project.Ipmi.Channel.Ipmb",
593                                        "/xyz/openbmc_project/Ipmi/Channel/Ipmb",
594                                        "org.openbmc.Ipmb", "sendRequest");
595     method.append(meAddress, netFn, lun, cmd, cmdData);
596 
597     auto reply = bus->call(method);
598     if (reply.is_method_error())
599     {
600         phosphor::logging::log<phosphor::logging::level::ERR>(
601             "Error reading from ME");
602         return -1;
603     }
604 
605     IpmbMethodType resp;
606     reply.read(resp);
607 
608     respData =
609         std::move(std::get<std::remove_reference_t<decltype(respData)>>(resp));
610 
611     if (DEBUG)
612     {
613         std::cout << "ME resp data: ";
614         for (auto d : respData)
615         {
616             std::cout << d << " ";
617         }
618         std::cout << "\n";
619     }
620 
621     return 0;
622 }
623 
udbg_get_info_page(uint8_t,uint8_t page,uint8_t * next,uint8_t * count,uint8_t * buffer)624 static int udbg_get_info_page(uint8_t, uint8_t page, uint8_t* next,
625                               uint8_t* count, uint8_t* buffer)
626 {
627     char line_buff[1000];
628     [[maybe_unused]] char* pres_dev = line_buff;
629     [[maybe_unused]] size_t pos = plat_get_fru_sel();
630     int ret;
631     std::string serialName = "SerialNumber";
632     std::string partName = "PartNumber";
633     std::string verDel = "VERSION=";
634     std::string verPath = "/etc/os-release";
635     size_t hostPosition = 0;
636     size_t maxPosition;
637 
638     if (page == 1)
639     {
640         // Only update frame data while getting page 1
641 
642         // initialize and clear frame
643         frame_info.init();
644         snprintf(frame_info.title, 32, "SYS_Info");
645 
646         bool platform = isMultiHostPlatform();
647         if (platform == true)
648         {
649             hostPosition = plat_get_fru_sel();
650         }
651 
652         getMaxHostPosition(maxPosition);
653         if (hostPosition == BMC_POSITION || hostInstances == "0")
654         {
655             std::string data = "FRU:" + getMotherBoardFruName();
656             frame_info.append(data);
657         }
658         else if (hostPosition != BMC_POSITION && hostPosition <= maxPosition)
659         {
660             std::string data = "FRU:slot" + std::to_string(hostPosition);
661             frame_info.append(data);
662         }
663 
664         // FRU
665         std::string data;
666         frame_info.append("SN:");
667         if (getFruData(data, serialName) != 0)
668         {
669             data = "Not Found";
670         }
671         frame_info.append(data, 1);
672         frame_info.append("PN:");
673         if (getFruData(data, partName) != 0)
674         {
675             data = "Not Found";
676         }
677         frame_info.append(data, 1);
678 
679         // LAN
680         getNetworkData(3, line_buff);
681         frame_info.append("BMC_IP:");
682         frame_info.append(line_buff, 1);
683         getNetworkData(59, line_buff);
684         frame_info.append("BMC_IPv6:");
685         frame_info.append(line_buff, 1);
686 
687         // BMC ver
688         std::ifstream file(verPath);
689         if (file)
690         {
691             std::string line;
692             while (std::getline(file, line))
693             {
694                 if (line.find(verDel) != std::string::npos)
695                 {
696                     std::string bmcVer = line.substr(verDel.size());
697                     frame_info.append("BMC_FW_ver:");
698                     frame_info.append(bmcVer, 1);
699                     break;
700                 }
701             }
702         }
703 
704         if (hostPosition != BMC_POSITION || hostInstances == "0")
705         {
706             // BIOS ver
707             std::string biosVer;
708             if (getBiosVer(biosVer, hostPosition) == 0)
709             {
710                 frame_info.append("BIOS_FW_ver:");
711                 frame_info.append(biosVer, 1);
712             }
713         }
714 
715         /* TBD: Board ID needs implementation */
716         // Board ID
717 
718         // Battery - Use Escape sequence
719         frame_info.append("Battery:");
720         frame_info.append(ESC_BAT "     ", 1);
721         // frame_info.append(&frame_info, esc_bat, 1);
722 
723         // MCU Version - Use Escape sequence
724         frame_info.append("MCUbl_ver:");
725         frame_info.append(ESC_MCU_BL_VER, 1);
726         frame_info.append("MCU_ver:");
727         frame_info.append(ESC_MCU_RUN_VER, 1);
728 
729         // Sys config present device
730         if (hostPosition != BMC_POSITION)
731         {
732             frame_info.append("Sys Conf. info:");
733 
734             // Dimm info
735             std::vector<std::string> data;
736             if (sysConfig(data, pos) == 0)
737             {
738                 for (auto& info : data)
739                 {
740                     frame_info.append(info, 1);
741                 }
742             }
743             else
744             {
745                 frame_info.append("Not Found", 1);
746             }
747 
748             // Processor info
749             std::string result;
750             if (procInfo(result, pos) != 0)
751             {
752                 result = "Not Found";
753             }
754             frame_info.append(result, 1);
755         }
756 
757     } // End of update frame
758 
759     if (page > frame_info.pages)
760     {
761         return -1;
762     }
763 
764     ret = frame_info.getPage(page, (char*)buffer, FRAME_PAGE_BUF_SIZE);
765     if (ret < 0)
766     {
767         *count = 0;
768         return -1;
769     }
770     *count = (uint8_t)ret;
771 
772     if (page < frame_info.pages)
773         *next = page + 1;
774     else
775         *next = 0xFF; // Set the value of next to 0xFF to indicate this is the
776                       // last page
777 
778     return 0;
779 }
780 
udbg_get_postcode(uint8_t,uint8_t page,uint8_t * next,uint8_t * count,uint8_t * buffer)781 static int udbg_get_postcode(uint8_t, uint8_t page, uint8_t* next,
782                              uint8_t* count, uint8_t* buffer)
783 {
784     // up to 70 codes can be displayed on 10 pages
785     static constexpr size_t maxPostcodes = 70;
786 
787     if (page == 1)
788     {
789         // Initialize and clear frame (example initialization)
790         frame_postcode.init();
791         snprintf(frame_postcode.title, 32, "POST CODE");
792         frame_postcode.max_page = 10;
793 
794         // Synchronously get D-Bus connection
795         auto bus = sdbusplus::bus::new_default();
796 
797         // Build D-Bus method call
798         auto method = bus.new_method_call(
799             "xyz.openbmc_project.State.Boot.PostCode0",  // Target service name
800             "/xyz/openbmc_project/State/Boot/PostCode0", // Object path
801             "xyz.openbmc_project.State.Boot.PostCode",   // Interface name
802             "GetPostCodes");                             // Method name
803 
804         method.append(uint16_t(1)); // Add method parameter, assuming it's page
805 
806         try
807         {
808             auto reply = bus.call(method); // Send synchronous method call
809 
810             // Read postcode value
811             std::vector<std::tuple<std::vector<uint8_t>, std::vector<uint8_t>>>
812                 postcodes;
813             reply.read(postcodes);
814 
815             // retrieve the latest postcodes
816             size_t numEntries = std::min(maxPostcodes, postcodes.size());
817             auto range = std::ranges::subrange(postcodes.rbegin(),
818                                                postcodes.rbegin() + numEntries);
819             for (const auto& [code, extra] : range)
820             {
821                 std::string result;
822                 result.reserve(2 * code.size());
823                 for (const auto& byte : code)
824                 {
825                     result += std::format("{:02X}", byte);
826                 }
827 
828                 frame_postcode.append(result);
829                 if (frame_postcode.lines >= maxPostcodes)
830                 {
831                     break;
832                 }
833             }
834         }
835         catch (const std::exception& e)
836         {
837             // Handle exceptions
838             std::cerr << "Error retrieving postcodes: " << e.what()
839                       << std::endl;
840             return -1;
841         }
842     }
843 
844     if (page > frame_postcode.pages)
845     {
846         return -1;
847     }
848 
849     int ret = frame_postcode.getPage(page, (char*)buffer, FRAME_PAGE_BUF_SIZE);
850     if (ret < 0)
851     {
852         *count = 0;
853         return -1;
854     }
855     *count = (uint8_t)ret;
856 
857     if (page < frame_postcode.pages)
858         *next = page + 1;
859     else
860         *next = 0xFF; // Set next to 0xFF to indicate last page
861     return 0;
862 }
863 
plat_udbg_get_frame_data(uint8_t frame,uint8_t page,uint8_t * next,uint8_t * count,uint8_t * buffer)864 int plat_udbg_get_frame_data(uint8_t frame, uint8_t page, uint8_t* next,
865                              uint8_t* count, uint8_t* buffer)
866 {
867     switch (frame)
868     {
869         case 1: // info_page
870             return udbg_get_info_page(frame, page, next, count, buffer);
871         case 2: // Extra Post Code
872             return udbg_get_postcode(frame, page, next, count, buffer);
873         default:
874             return -1;
875     }
876 }
877 
panel_main(size_t item)878 static panel panel_main(size_t item)
879 {
880     // Update item list when select item 0
881     switch (item)
882     {
883         case 1:
884             return panels[std::to_underlying(panel::BOOT_ORDER)].select(0);
885         case 2:
886             return panels[std::to_underlying(panel::POWER_POLICY)].select(0);
887         default:
888             return panel::MAIN;
889     }
890 }
891 
panel_boot_order(size_t selectedItemIndex)892 static panel panel_boot_order(size_t selectedItemIndex)
893 {
894     static constexpr size_t sizeBootOrder = 6;
895     static constexpr size_t bootValid = 0x80;
896 
897     std::vector<uint8_t> bootSeq;
898 
899     ctrl_panel& bootOrderPanel = panels[std::to_underlying(panel::BOOT_ORDER)];
900 
901     size_t pos = plat_get_fru_sel();
902 
903     if (pos == FRU_ALL)
904     {
905         bootOrderPanel.item_num = 0;
906         return panel::BOOT_ORDER;
907     }
908 
909     auto [bootObjPath, hostName] = ipmi::boot::objPath(pos);
910     ipmi::boot::getBootOrder(bootObjPath, bootSeq, hostName);
911 
912     uint8_t& bootMode = bootSeq.front();
913 
914     // One item is selected to set a new boot sequence.
915     // The selected item become the first boot order.
916     if (selectedItemIndex > 0 && selectedItemIndex < sizeBootOrder)
917     {
918         // Move the selected item to second element (the first one is boot mode)
919         std::rotate(bootSeq.begin() + 1, bootSeq.begin() + selectedItemIndex,
920                     bootSeq.begin() + selectedItemIndex + 1);
921 
922         bootMode |= bootValid;
923         try
924         {
925             ipmi::boot::setBootOrder(bootObjPath, bootSeq, hostName);
926         }
927         catch (const std::exception& e)
928         {
929             lg2::error("Fail to set boot order : {ERROR}", "ERROR", e);
930         }
931 
932         // refresh items
933         return bootOrderPanel.select(0);
934     }
935 
936     // '*': boot flags valid, BIOS has not yet read
937     bootOrderPanel.item_str[0] =
938         std::string("Boot Order") + ((bootMode & bootValid) ? "*" : "");
939 
940     static const std::unordered_map<uint8_t, const char*>
941         bootOrderMappingTable = {
942             {0x00, " USB device"}, {0x01, " Network v4"}, {0x02, " SATA HDD"},
943             {0x03, " SATA-CDROM"}, {0x04, " Other"},      {0x09, " Network v6"},
944         };
945 
946     size_t validItem = 0;
947     for (size_t i = 1; i < sizeBootOrder; i++)
948     {
949         auto find = bootOrderMappingTable.find(bootSeq[i]);
950         if (find == bootOrderMappingTable.end())
951         {
952             lg2::error("Unknown boot order : {BOOTORDER}", "BOOTORDER",
953                        bootSeq[i]);
954             break;
955         }
956 
957         bootOrderPanel.item_str[i] = find->second;
958 
959         validItem++;
960     }
961 
962     bootOrderPanel.item_num = validItem;
963     return panel::BOOT_ORDER;
964 }
965 
panel_power_policy(size_t)966 static panel panel_power_policy(size_t)
967 {
968 /* To be cleaned */
969 #if 0
970     uint8_t buff[32] = {0};
971     uint8_t res_len;
972     size_t pos = plat_get_fru_sel();
973     uint8_t policy;
974     uint8_t pwr_policy_item_map[3] = {POWER_CFG_ON, POWER_CFG_LPS,
975                                       POWER_CFG_OFF};
976 
977     if (pos != FRU_ALL)
978     {
979         if (item > 0 && item <= sizeof(pwr_policy_item_map))
980         {
981             policy = pwr_policy_item_map[item - 1];
982             pal_set_power_restore_policy(pos, &policy, nullptr);
983         }
984         pal_get_chassis_status(pos, nullptr, buff, &res_len);
985         policy = (((uint8_t)buff[0]) >> 5) & 0x7;
986         snprintf(panels[PANEL_POWER_POLICY].item_str[1], 32, "%cPower On",
987                  policy == POWER_CFG_ON ? '*' : ' ');
988         snprintf(panels[PANEL_POWER_POLICY].item_str[2], 32, "%cLast State",
989                  policy == POWER_CFG_LPS ? '*' : ' ');
990         snprintf(panels[PANEL_POWER_POLICY].item_str[3], 32, "%cPower Off",
991                  policy == POWER_CFG_OFF ? '*' : ' ');
992         panels[PANEL_POWER_POLICY].item_num = 3;
993     }
994     else
995     {
996         panels[PANEL_POWER_POLICY].item_num = 0;
997     }
998 #endif
999     return panel::POWER_POLICY;
1000 }
1001 
plat_udbg_control_panel(uint8_t cur_panel,uint8_t operation,uint8_t item,uint8_t * count,uint8_t * buffer)1002 ipmi_ret_t plat_udbg_control_panel(uint8_t cur_panel, uint8_t operation,
1003                                    uint8_t item, uint8_t* count,
1004                                    uint8_t* buffer)
1005 {
1006     if (cur_panel > panelNum || cur_panel < std::to_underlying(panel::MAIN))
1007         return ipmi::ccParmOutOfRange;
1008 
1009     // No more item; End of item list
1010     if (item > panels[cur_panel].item_num)
1011         return ipmi::ccParmOutOfRange;
1012 
1013     switch (operation)
1014     {
1015         case 0: // Get Description
1016             break;
1017         case 1: // Select item
1018             cur_panel = std::to_underlying(panels[cur_panel].select(item));
1019             item = 0;
1020             break;
1021         case 2: // Back
1022             cur_panel = std::to_underlying(panels[cur_panel].parent);
1023             item = 0;
1024             break;
1025         default:
1026             return ipmi::ccParmOutOfRange;
1027     }
1028 
1029     buffer[0] = cur_panel;
1030     buffer[1] = item;
1031     buffer[2] = std::size(panels[cur_panel].item_str[item]);
1032 
1033     if (buffer[2] > 0 && (buffer[2] + 3u) < FRAME_PAGE_BUF_SIZE)
1034     {
1035         std::memcpy(&buffer[3], (panels[cur_panel].item_str[item]).c_str(),
1036                     buffer[2]);
1037     }
1038     *count = buffer[2] + 3;
1039     return ipmi::ccSuccess;
1040 }
1041 
1042 } // end of namespace ipmi
1043