summaryrefslogtreecommitdiffhomepage
path: root/src/coreboot-start.c
blob: eeeaabc4a07db9df5fb1b7aa413690ef5b96a9b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* -------------------------------------------

  Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved.

------------------------------------------- */

#include <lib/boot.h>
#include <lib/mp-bit.h>
#include <lib/partition-map.h>
#include <lib/pci-tree.h>

/// BUGS: 0

/////////////////////////////////////////////////////////////////////////////////////////

// @name start.c //

// @brief Start file // This is where the firmware starts it's initialization //
// code. //

// @author Amlal EL Mahrouss //

/////////////////////////////////////////////////////////////////////////////////////////

extern void cb_start_context(uintptr_t);
extern void cb_start_rom(void);

extern int cb_boot_processor_ready;

/// @brief hardware thread counter (rv64 only)
#ifdef __COMPILE_RISCV__
uint64_t __cb_hart_counter = 0UL;
#endif

/// @brief Start executing the firmware.
/// @param
void cb_start_exec(void) {
#ifndef __COMPILE_RISCV__
  static uint64_t __cb_hart_counter = 0UL;
#endif

  ++__cb_hart_counter;

  uintptr_t hart = __cb_hart_counter;

  // let the hart 0 init our stuff.
  if (hart == 0) {
    cb_put_string("CB> Welcome to CoreBoot, (c) Amlal EL Mahrouss. Built the ");
    cb_put_string(__DATE__);
    cb_put_string("\r\r\n");

#ifdef __COMPILE_POWERPC__
    cb_put_string("CB> CPU: PowerPC 64-bit Based SoC.\r\r\n");
#endif  // __COMPILE_POWERPC__

#ifdef __COMPILE_AMD64__
    cb_put_string("CB> CPU: x64 Based SoC.\r\r\n");
#endif  // __COMPILE_AMD64__

#ifdef __COMPILE_ARM64__
    cb_put_string("CB> CPU: AArch64 Based SoC.\r\r\n");
#endif  // __COMPILE_ARM64__

#ifdef __COMPILE_ARM32__
    cb_put_string("CB> CPU: AArch32 Based SoC.\r\r\n");
#endif  // __COMPILE_ARM64__

#ifdef __COMPILE_RISCV__
    cb_put_string("CB> CPU: RV64 Based SoC.\r\r\n");
#endif  // __COMPILE_RISCV__
  }

  /// @brief Boots here if LX header matches what we except.

  volatile struct cb_boot_header* boot_hdr = (volatile struct cb_boot_header*) (CB_FLASH_BASE_ADDR);

  /**
    boot if:
      - ident matches.
      - version matches.
  */

  if (boot_hdr->h_mag[0] == CB_BOOT_MAG_0 && boot_hdr->h_mag[1] == CB_BOOT_MAG_1) {
    uint32_t rev = boot_hdr->h_revision;

#ifdef __aarch64__
    rev = __builtin_bswap32(rev);
#endif

    if (rev != CB_BOOT_VER) {
      if (hart == 0) {
        cb_put_string("CB> Can't Boot the StageTwo, LX invalid signature. (CB0003)\r\n");
      }
    } else {
      if (hart == 0) {
        cb_pci_append_tree("@stage2-lx", (cb_pci_num_t) boot_hdr, sizeof(struct cb_boot_header));

        cb_put_string("CB> Executing StageTwo: ");
        cb_put_string((const char*) boot_hdr->h_name);
        cb_put_char('\r');
        cb_put_char('\n');

        // printf("CB> address: %x\n", boot_hdr->h_start_address);
      }

      if (boot_hdr->h_start_address != 0) {
        cb_boot_processor_ready = 1;
        cb_start_context(boot_hdr->h_start_address);
      }

      cb_put_string("CB> StageTwo has returned? (CB0002)\r\n");
    }
  } else {
    cb_put_string("CB> Trying EPM partition...\r\n");

    part_block_t* blk =
        cb_parse_partition_block_at((voidptr_t) CB_BOOT_ADDR, EPM_PART_BLK_SZ, 0);

    if (blk) {
      cb_pci_append_tree("@stage2-epm", (cb_pci_num_t) blk, sizeof(part_block_t) * blk->num_blocks);

      size_t indx = 0;
      size_t end_lba, start_lba, sector_sz;

      while (indx < blk->num_blocks) {
        if (cb_parse_partition_block_data_at(blk, EPM_PART_BLK_SZ * blk->num_blocks, indx, &end_lba,
                                             &start_lba, &sector_sz) == no) {
          ++indx;
          continue;
        }

        cb_boot_processor_ready = 1;
        cb_start_context((uintptr_t) (voidptr_t) blk + start_lba);

        if (hart == 1) {
          cb_put_string("CB> Can't boot to StageTwo. (CB0001)\r\n");
        }
      }
    }

    if (hart == 1) {
      cb_put_string(
          "CB> Can't boot to StageTwo via EPM, no bootable partition blocks found. (CB0001)\r\n");
    }
  }

  /// end of TODO

  if (hart > 1) {
    while (yes) {
      if (__cb_hart_counter == 0) {
        cb_restart_machine();
      }
    }
  }
}