planetcore.c 2.6 KB

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