Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * PlanetCore configuration data support functions
0004  *
0005  * Author: Scott Wood <scottwood@freescale.com>
0006  *
0007  * Copyright (c) 2007 Freescale Semiconductor, Inc.
0008  */
0009 
0010 #include "stdio.h"
0011 #include "stdlib.h"
0012 #include "ops.h"
0013 #include "planetcore.h"
0014 #include "io.h"
0015 
0016 /* PlanetCore passes information to the OS in the form of
0017  * a table of key=value strings, separated by newlines.
0018  *
0019  * The list is terminated by an empty string (i.e. two
0020  * consecutive newlines).
0021  *
0022  * To make it easier to parse, we first convert all the
0023  * newlines into null bytes.
0024  */
0025 
0026 void planetcore_prepare_table(char *table)
0027 {
0028     do {
0029         if (*table == '\n')
0030             *table = 0;
0031 
0032         table++;
0033     } while (*(table - 1) || *table != '\n');
0034 
0035     *table = 0;
0036 }
0037 
0038 const char *planetcore_get_key(const char *table, const char *key)
0039 {
0040     int keylen = strlen(key);
0041 
0042     do {
0043         if (!strncmp(table, key, keylen) && table[keylen] == '=')
0044             return table + keylen + 1;
0045 
0046         table += strlen(table) + 1;
0047     } while (strlen(table) != 0);
0048 
0049     return NULL;
0050 }
0051 
0052 int planetcore_get_decimal(const char *table, const char *key, u64 *val)
0053 {
0054     const char *str = planetcore_get_key(table, key);
0055     if (!str)
0056         return 0;
0057 
0058     *val = strtoull(str, NULL, 10);
0059     return 1;
0060 }
0061 
0062 int planetcore_get_hex(const char *table, const char *key, u64 *val)
0063 {
0064     const char *str = planetcore_get_key(table, key);
0065     if (!str)
0066         return 0;
0067 
0068     *val = strtoull(str, NULL, 16);
0069     return 1;
0070 }
0071 
0072 static u64 mac_table[4] = {
0073     0x000000000000,
0074     0x000000800000,
0075     0x000000400000,
0076     0x000000c00000,
0077 };
0078 
0079 void planetcore_set_mac_addrs(const char *table)
0080 {
0081     u8 addr[4][6];
0082     u64 int_addr;
0083     u32 i;
0084     int j;
0085 
0086     if (!planetcore_get_hex(table, PLANETCORE_KEY_MAC_ADDR, &int_addr))
0087         return;
0088 
0089     for (i = 0; i < 4; i++) {
0090         u64 this_dev_addr = (int_addr & ~0x000000c00000) |
0091                             mac_table[i];
0092 
0093         for (j = 5; j >= 0; j--) {
0094             addr[i][j] = this_dev_addr & 0xff;
0095             this_dev_addr >>= 8;
0096         }
0097 
0098         dt_fixup_mac_address(i, addr[i]);
0099     }
0100 }
0101 
0102 static char prop_buf[MAX_PROP_LEN];
0103 
0104 void planetcore_set_stdout_path(const char *table)
0105 {
0106     char *path;
0107     const char *label;
0108     void *node, *chosen;
0109 
0110     label = planetcore_get_key(table, PLANETCORE_KEY_SERIAL_PORT);
0111     if (!label)
0112         return;
0113 
0114     node = find_node_by_prop_value_str(NULL, "linux,planetcore-label",
0115                                        label);
0116     if (!node)
0117         return;
0118 
0119     path = get_path(node, prop_buf, MAX_PROP_LEN);
0120     if (!path)
0121         return;
0122 
0123     chosen = finddevice("/chosen");
0124     if (!chosen)
0125         chosen = create_node(NULL, "chosen");
0126     if (!chosen)
0127         return;
0128 
0129     setprop_str(chosen, "linux,stdout-path", path);
0130 }