Skip to content

Commit e7e1153

Browse files
authored
Merge pull request #83 from pcengines/new-cbfs-api-support
WIP: Support for the new CBFS API
2 parents 1d3b469 + a7c8b66 commit e7e1153

File tree

1 file changed

+43
-46
lines changed

1 file changed

+43
-46
lines changed

sortbootorder.c

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1717
*/
1818

19-
#include <libpayload.h>
19+
#include <boot_device.h>
2020
#include <cbfs.h>
21+
#include <cbfs_glue.h>
2122
#include <coreboot_tables.h>
2223
#include <curses.h>
2324
#include <flash_access.h>
25+
#include <libpayload.h>
26+
#include <rtc_clock_menu.h>
2427
#include <sec_reg_menu.h>
2528
#include <spi/spi_lock_menu.h>
26-
#include <rtc_clock_menu.h>
2729

2830
#include "version.h"
2931

@@ -502,7 +504,7 @@ static void show_boot_device_list(char buffer[MAX_DEVICES][MAX_LENGTH],
502504
(sd3_toggle) ? "Enabled" : "Disabled");
503505
printf(" g Reverse order of PCI addresses - Currently %s\n",
504506
(pciereverse_toggle) ? "Enabled" : "Disabled");
505-
507+
506508
#ifndef COREBOOT_LEGACY
507509
printf(" v IOMMU - Currently %s\n",
508510
(iommu_toggle) ? "Enabled" : "Disabled");
@@ -521,31 +523,26 @@ static void show_boot_device_list(char buffer[MAX_DEVICES][MAX_LENGTH],
521523
static int fetch_bootorder_from_cbfs(char destination[MAX_DEVICES][MAX_LENGTH],
522524
u8 *line_count)
523525
{
524-
char *cbfs_dat;
525-
struct cbfs_handle *bootorder_handle;
526+
void *bootorder_mapping;
526527
size_t cbfs_length;
527528

528-
bootorder_handle = cbfs_get_handle(CBFS_DEFAULT_MEDIA, BOOTORDER_FILE);
529-
530-
flash_address = (void *)(bootorder_handle->media_offset +
531-
bootorder_handle->content_offset);
532-
533-
if ((u32)flash_address & 0xfff)
534-
printf("Warning: The bootorder file is not 4k aligned!\n");
535-
536-
cbfs_dat = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, BOOTORDER_FILE,
537-
CBFS_TYPE_RAW, &cbfs_length);
529+
bootorder_mapping = cbfs_map(BOOTORDER_FILE, &cbfs_length);
538530

539-
if (!cbfs_dat) {
531+
if (!bootorder_mapping) {
540532
printf("Error: file [%s] not found!\n", BOOTORDER_FILE);
541533
return -1;
542534
}
543535

544-
if (cbfs_dat[0] == 0xFF || cbfs_dat[0] == 0x00) {
536+
printf("The bootorder file size is %zu!\n", cbfs_length);
537+
538+
if ((u32)bootorder_mapping & 0xfff)
539+
printf("Warning: The bootorder file is not 4k aligned!\n");
540+
541+
if (*(u16 *)bootorder_mapping == 0xFFFF || *(u16 *)bootorder_mapping == 0x0000) {
545542
printf("Error: bootorder is empty!\n");
546543
return -1;
547544
}
548-
memcpy(bootorder_data, cbfs_dat, cbfs_length);
545+
memcpy(bootorder_data, bootorder_mapping, cbfs_length);
549546

550547
if (fetch_file_from_cbfs(BOOTORDER_FILE, destination, line_count))
551548
return -1;
@@ -557,37 +554,35 @@ static int fetch_bootorder(char destination[MAX_DEVICES][MAX_LENGTH],
557554
u8 *line_count)
558555
{
559556
char tmp;
560-
int offset = 0, char_cnt = 0;
561-
u32 bootorder_offset, bootorder_size;
562-
struct cbfs_media default_media;
563-
struct cbfs_media *media = &default_media;
564-
557+
u16 offset = 0, char_cnt = 0;
558+
static struct cbfs_boot_device rw;
565559
u32 rom_begin = (0xFFFFFFFF - lib_sysinfo.spi_flash.size) + 1;
566560

567-
int rc = fmap_region_by_name(lib_sysinfo.fmap_offset, "BOOTORDER",
568-
&bootorder_offset, &bootorder_size);
569-
if (rc == -1) {
561+
if (fmap_locate_area("BOOTORDER", &rw.dev.offset, &rw.dev.size)) {
562+
printf("BOOTORDER area not found, fetching from CBFS...\n");
570563
return fetch_bootorder_from_cbfs(destination, line_count);
571-
} else {
572-
flash_address = (void *)(rom_begin + bootorder_offset);
573-
574-
if (init_default_cbfs_media(media))
575-
return -1;
576-
577-
media->open(media);
578-
if (!media->read(media, bootorder_data, bootorder_offset,
579-
bootorder_size))
580-
return -1;
581-
media->close(media);
582564
}
583565

566+
flash_address = (void *)(rom_begin + rw.dev.offset);
584567
if ((u32)flash_address & 0xfff)
585568
printf("Warning: The bootorder file is not 4k aligned!\n");
586569

587-
if (bootorder_data[0] == 0xFF || bootorder_data[0] == 0x00)
570+
if (!rw.dev.size) {
571+
printf("BOOTORDER area size is zero, fetching from CBFS...\n");
588572
return fetch_bootorder_from_cbfs(destination, line_count);
573+
}
589574

590-
//count up the lines and display
575+
if (boot_device_read(bootorder_data, rw.dev.offset, rw.dev.size) != rw.dev.size) {
576+
printf("Failed to read bootorder data, fetching from CBFS...\n");
577+
return fetch_bootorder_from_cbfs(destination, line_count);
578+
}
579+
580+
if (bootorder_data[0] == 0xFF || bootorder_data[0] == 0x00) {
581+
printf("Invalid bootorder region data, fetching from CBFS: (0x%x)...\n", bootorder_data[0] );
582+
return fetch_bootorder_from_cbfs(destination, line_count);
583+
}
584+
585+
// Count up the lines and display
591586
*line_count = 0;
592587
while (1) {
593588
tmp = *(bootorder_data + offset++);
@@ -597,21 +592,21 @@ static int fetch_bootorder(char destination[MAX_DEVICES][MAX_LENGTH],
597592
char_cnt = 0;
598593
}
599594
else if ((tmp == 0xFF) || (tmp == NUL) ||
600-
(offset > bootorder_size))
595+
(offset > rw.dev.size))
601596
break;
602597
else
603598
char_cnt++;
604599

605600
if (*line_count > MAX_DEVICES) {
606-
printf("aborting due to excessive line_count\n");
601+
printf("Aborting due to excessive line count: %d\n", *line_count);
607602
break;
608603
}
609604
if (char_cnt > MAX_LENGTH) {
610-
printf("aborting due to excessive char count\n");
605+
printf("Aborting due to excessive char count: %d\n", char_cnt);
611606
break;
612607
}
613-
if (offset > (MAX_LENGTH*MAX_DEVICES)) {
614-
printf("aborting due to excessive cbfs ptr length\n");
608+
if (offset > (MAX_LENGTH * MAX_DEVICES)) {
609+
printf("Aborting due to excessive cbfs ptr length: %d\n", offset);
615610
break;
616611
}
617612
}
@@ -646,7 +641,7 @@ static int fetch_file_from_cbfs(char *filename,
646641
int cbfs_offset = 0, char_cnt = 0;
647642
size_t cbfs_length;
648643

649-
cbfs_dat = (char *) cbfs_get_file_content( CBFS_DEFAULT_MEDIA, filename, CBFS_TYPE_RAW, &cbfs_length );
644+
cbfs_dat = (char *) cbfs_map(filename, &cbfs_length);
650645
if (!cbfs_dat) {
651646
printf("Error: file [%s] not found!\n", filename);
652647
return 1;
@@ -679,6 +674,8 @@ static int fetch_file_from_cbfs(char *filename,
679674
break;
680675
}
681676
}
677+
678+
cbfs_unmap(cbfs_dat);
682679
return 0;
683680
}
684681

@@ -764,7 +761,7 @@ static void update_wdg_timeout(char buffer[MAX_DEVICES][MAX_LENGTH],
764761
buffer[*max_lines][strlen(tag)+5] = '\n';
765762
buffer[*max_lines][strlen(tag)+6] = '0';
766763
(*max_lines)++;
767-
}
764+
}
768765
}
769766
#endif
770767

0 commit comments

Comments
 (0)