1
0

run_ipxe.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* This file is part of asmc, a bootstrapping OS with minimal seed
  2. Copyright (C) 2019 Giovanni Mascellani <gio@debian.org>
  3. https://gitlab.com/giomasce/asmc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. /* Stuff is a bit complicated here, because we are already in iPXE
  15. namespace, so we cannot use asmc's stdlib, because it would clash
  16. with iPXE. OTOH, iPXE stuff is not yet initialized. So we just pass
  17. a few pointers with the needed library functions and data. Clearly,
  18. type definitions in this file must be manually kept in sync with
  19. their original versions. */
  20. #include "ipxe_asmc.h"
  21. #include "ipxe_handover.h"
  22. ipxe_handover *ih;
  23. int pre_main(ipxe_handover *ih_) {
  24. ih = ih_;
  25. return main();
  26. }
  27. static table_sect *find_section(const char *name) {
  28. int i;
  29. for (i = 0; i < ih->sects_num; i++) {
  30. const char *name2 = ih->sects[i].name;
  31. int j;
  32. for (j = 0; ; j++) {
  33. if (name[j] != name2[j]) break;
  34. if (name2[j] == '\0') {
  35. return &ih->sects[i];
  36. }
  37. }
  38. }
  39. return 0;
  40. }
  41. // Just a random number, but such that subtracting any reasonable
  42. // structure size does not wrap around (otherwise reverse for cycles
  43. // choke).
  44. #define EMPTY_LIST ((void*) 0x100000)
  45. void *get_table_start(const char *name) {
  46. table_sect *sect = find_section(name);
  47. if (!sect) return EMPTY_LIST;
  48. return sect->data;
  49. }
  50. void *get_table_end(const char *name) {
  51. table_sect *sect = find_section(name);
  52. if (!sect) return EMPTY_LIST;
  53. return sect->data + sect->len;
  54. }
  55. void *asmc_malloc(size_t size) {
  56. return ih->malloc(size);
  57. }
  58. void asmc_free(void *ptr) {
  59. ih->free(ptr);
  60. }
  61. void *asmc_realloc(void *ptr, size_t size) {
  62. return ih->realloc(ptr, size);
  63. }
  64. void push_to_asmc(void *msg) {
  65. ipxe_list_push(ih, &ih->from_ipxe, msg);
  66. }
  67. void *pop_from_asmc(void) {
  68. return ipxe_list_pop(ih, &ih->to_ipxe);
  69. }
  70. void coro_yield(void) {
  71. ih->coro_yield();
  72. }