planetcore.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PlanetCore configuration data support functions
  4. *
  5. * Author: Scott Wood <scottwood@freescale.com>
  6. *
  7. * Copyright (c) 2007 Freescale Semiconductor, Inc.
  8. */
  9. #include "stdio.h"
  10. #include "stdlib.h"
  11. #include "ops.h"
  12. #include "planetcore.h"
  13. #include "io.h"
  14. /* PlanetCore passes information to the OS in the form of
  15. * a table of key=value strings, separated by newlines.
  16. *
  17. * The list is terminated by an empty string (i.e. two
  18. * consecutive newlines).
  19. *
  20. * To make it easier to parse, we first convert all the
  21. * newlines into null bytes.
  22. */
  23. void planetcore_prepare_table(char *table)
  24. {
  25. do {
  26. if (*table == '\n')
  27. *table = 0;
  28. table++;
  29. } while (*(table - 1) || *table != '\n');
  30. *table = 0;
  31. }
  32. const char *planetcore_get_key(const char *table, const char *key)
  33. {
  34. int keylen = strlen(key);
  35. do {
  36. if (!strncmp(table, key, keylen) && table[keylen] == '=')
  37. return table + keylen + 1;
  38. table += strlen(table) + 1;
  39. } while (strlen(table) != 0);
  40. return NULL;
  41. }
  42. int planetcore_get_decimal(const char *table, const char *key, u64 *val)
  43. {
  44. const char *str = planetcore_get_key(table, key);
  45. if (!str)
  46. return 0;
  47. *val = strtoull(str, NULL, 10);
  48. return 1;
  49. }
  50. int planetcore_get_hex(const char *table, const char *key, u64 *val)
  51. {
  52. const char *str = planetcore_get_key(table, key);
  53. if (!str)
  54. return 0;
  55. *val = strtoull(str, NULL, 16);
  56. return 1;
  57. }
  58. static u64 mac_table[4] = {
  59. 0x000000000000,
  60. 0x000000800000,
  61. 0x000000400000,
  62. 0x000000c00000,
  63. };
  64. void planetcore_set_mac_addrs(const char *table)
  65. {
  66. u8 addr[4][6];
  67. u64 int_addr;
  68. u32 i;
  69. int j;
  70. if (!planetcore_get_hex(table, PLANETCORE_KEY_MAC_ADDR, &int_addr))
  71. return;
  72. for (i = 0; i < 4; i++) {
  73. u64 this_dev_addr = (int_addr & ~0x000000c00000) |
  74. mac_table[i];
  75. for (j = 5; j >= 0; j--) {
  76. addr[i][j] = this_dev_addr & 0xff;
  77. this_dev_addr >>= 8;
  78. }
  79. dt_fixup_mac_address(i, addr[i]);
  80. }
  81. }
  82. static char prop_buf[MAX_PROP_LEN];
  83. void planetcore_set_stdout_path(const char *table)
  84. {
  85. char *path;
  86. const char *label;
  87. void *node, *chosen;
  88. label = planetcore_get_key(table, PLANETCORE_KEY_SERIAL_PORT);
  89. if (!label)
  90. return;
  91. node = find_node_by_prop_value_str(NULL, "linux,planetcore-label",
  92. label);
  93. if (!node)
  94. return;
  95. path = get_path(node, prop_buf, MAX_PROP_LEN);
  96. if (!path)
  97. return;
  98. chosen = finddevice("/chosen");
  99. if (!chosen)
  100. chosen = create_node(NULL, "chosen");
  101. if (!chosen)
  102. return;
  103. setprop_str(chosen, "linux,stdout-path", path);
  104. }