iPXE
fdt.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2019 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <string.h>
00027 #include <errno.h>
00028 #include <assert.h>
00029 #include <byteswap.h>
00030 #include <ipxe/netdevice.h>
00031 #include <ipxe/fdt.h>
00032 
00033 /** @file
00034  *
00035  * Flattened Device Tree
00036  *
00037  */
00038 
00039 /** The system flattened device tree (if present) */
00040 static struct fdt fdt;
00041 
00042 /** A position within a device tree */
00043 struct fdt_cursor {
00044         /** Offset within structure block */
00045         unsigned int offset;
00046         /** Tree depth */
00047         int depth;
00048 };
00049 
00050 /** A lexical descriptor */
00051 struct fdt_descriptor {
00052         /** Node or property name (if applicable) */
00053         const char *name;
00054         /** Property data (if applicable) */
00055         const void *data;
00056         /** Length of property data (if applicable) */
00057         size_t len;
00058 };
00059 
00060 /**
00061  * Check if device tree exists
00062  *
00063  * @v has_fdt           Device tree exists
00064  */
00065 static inline __attribute__ (( always_inline )) int fdt_exists ( void ) {
00066 
00067         return ( fdt.hdr != NULL );
00068 }
00069 
00070 /**
00071  * Traverse device tree
00072  *
00073  * @v pos               Position within device tree
00074  * @v desc              Lexical descriptor to fill in
00075  * @ret rc              Return status code
00076  */
00077 static int fdt_traverse ( struct fdt_cursor *pos,
00078                           struct fdt_descriptor *desc ) {
00079         const fdt_token_t *token;
00080         const void *data;
00081         const struct fdt_prop *prop;
00082         unsigned int name_off;
00083         size_t remaining;
00084         size_t len;
00085 
00086         /* Sanity checks */
00087         assert ( pos->offset < fdt.len );
00088         assert ( ( pos->offset & ( FDT_STRUCTURE_ALIGN - 1 ) ) == 0 );
00089 
00090         /* Clear descriptor */
00091         memset ( desc, 0, sizeof ( *desc ) );
00092 
00093         /* Locate token and calculate remaining space */
00094         token = ( fdt.raw + fdt.structure + pos->offset );
00095         remaining = ( fdt.len - pos->offset );
00096         if ( remaining < sizeof ( *token ) ) {
00097                 DBGC ( &fdt, "FDT truncated tree at +%#04x\n", pos->offset );
00098                 return -EINVAL;
00099         }
00100         remaining -= sizeof ( *token );
00101         data = ( ( ( const void * ) token ) + sizeof ( *token ) );
00102         len = 0;
00103 
00104         /* Handle token */
00105         switch ( *token ) {
00106 
00107         case cpu_to_be32 ( FDT_BEGIN_NODE ):
00108 
00109                 /* Start of node */
00110                 desc->name = data;
00111                 len = ( strnlen ( desc->name, remaining ) + 1 /* NUL */ );
00112                 if ( remaining < len ) {
00113                         DBGC ( &fdt, "FDT unterminated node name at +%#04x\n",
00114                                pos->offset );
00115                         return -EINVAL;
00116                 }
00117                 pos->depth++;
00118                 break;
00119 
00120         case cpu_to_be32 ( FDT_END_NODE ):
00121 
00122                 /* End of node */
00123                 if ( pos->depth < 0 ) {
00124                         DBGC ( &fdt, "FDT spurious node end at +%#04x\n",
00125                                pos->offset );
00126                         return -EINVAL;
00127                 }
00128                 pos->depth--;
00129                 if ( pos->depth < 0 ) {
00130                         /* End of (sub)tree */
00131                         return -ENOENT;
00132                 }
00133                 break;
00134 
00135         case cpu_to_be32 ( FDT_PROP ):
00136 
00137                 /* Property */
00138                 prop = data;
00139                 if ( remaining < sizeof ( *prop ) ) {
00140                         DBGC ( &fdt, "FDT truncated property at +%#04x\n",
00141                                pos->offset );
00142                         return -EINVAL;
00143                 }
00144                 desc->data = ( ( ( const void * ) prop ) + sizeof ( *prop ) );
00145                 desc->len = be32_to_cpu ( prop->len );
00146                 len = ( sizeof ( *prop ) + desc->len );
00147                 if ( remaining < len ) {
00148                         DBGC ( &fdt, "FDT overlength property at +%#04x\n",
00149                                pos->offset );
00150                         return -EINVAL;
00151                 }
00152                 name_off = be32_to_cpu ( prop->name_off );
00153                 if ( name_off > fdt.strings_len ) {
00154                         DBGC ( &fdt, "FDT property name outside strings "
00155                                "block at +%#04x\n", pos->offset );
00156                         return -EINVAL;
00157                 }
00158                 desc->name = ( fdt.raw + fdt.strings + name_off );
00159                 break;
00160 
00161         case cpu_to_be32 ( FDT_NOP ):
00162 
00163                 /* Do nothing */
00164                 break;
00165 
00166         default:
00167 
00168                 /* Unrecognised or unexpected token */
00169                 DBGC ( &fdt, "FDT unexpected token %#08x at +%#04x\n",
00170                        be32_to_cpu ( *token ), pos->offset );
00171                 return -EINVAL;
00172         }
00173 
00174         /* Update cursor */
00175         len = ( ( len + FDT_STRUCTURE_ALIGN - 1 ) &
00176                 ~( FDT_STRUCTURE_ALIGN - 1 ) );
00177         pos->offset += ( sizeof ( *token ) + len );
00178 
00179         /* Sanity checks */
00180         assert ( pos->offset <= fdt.len );
00181 
00182         return 0;
00183 }
00184 
00185 /**
00186  * Find child node
00187  *
00188  * @v offset            Starting node offset
00189  * @v name              Node name
00190  * @v child             Child node offset to fill in
00191  * @ret rc              Return status code
00192  */
00193 static int fdt_child ( unsigned int offset, const char *name,
00194                        unsigned int *child ) {
00195         struct fdt_cursor pos;
00196         struct fdt_descriptor desc;
00197         unsigned int orig_offset;
00198         int rc;
00199 
00200         /* Record original offset (for debugging) */
00201         orig_offset = offset;
00202 
00203         /* Initialise cursor */
00204         pos.offset = offset;
00205         pos.depth = -1;
00206 
00207         /* Find child node */
00208         while ( 1 ) {
00209 
00210                 /* Record current offset */
00211                 *child = pos.offset;
00212 
00213                 /* Traverse tree */
00214                 if ( ( rc = fdt_traverse ( &pos, &desc ) ) != 0 ) {
00215                         DBGC ( &fdt, "FDT +%#04x has no child node \"%s\": "
00216                                "%s\n", orig_offset, name, strerror ( rc ) );
00217                         return rc;
00218                 }
00219 
00220                 /* Check for matching immediate child node */
00221                 if ( ( pos.depth == 1 ) && desc.name && ( ! desc.data ) ) {
00222                         DBGC2 ( &fdt, "FDT +%#04x has child node \"%s\"\n",
00223                                 orig_offset, desc.name );
00224                         if ( strcmp ( name, desc.name ) == 0 ) {
00225                                 DBGC2 ( &fdt, "FDT +%#04x found child node "
00226                                         "\"%s\" at +%#04x\n", orig_offset,
00227                                         desc.name, *child );
00228                                 return 0;
00229                         }
00230                 }
00231         }
00232 }
00233 
00234 /**
00235  * Find node by path
00236  *
00237  * @v path              Node path
00238  * @v offset            Offset to fill in
00239  * @ret rc              Return status code
00240  */
00241 int fdt_path ( const char *path, unsigned int *offset ) {
00242         char *tmp = ( ( char * ) path );
00243         char *del;
00244         int rc;
00245 
00246         /* Initialise offset */
00247         *offset = 0;
00248 
00249         /* Traverse tree one path segment at a time */
00250         while ( *tmp ) {
00251 
00252                 /* Skip any leading '/' */
00253                 while ( *tmp == '/' )
00254                         tmp++;
00255 
00256                 /* Find next '/' delimiter and convert to NUL */
00257                 del = strchr ( tmp, '/' );
00258                 if ( del )
00259                         *del = '\0';
00260 
00261                 /* Find child and restore delimiter */
00262                 rc = fdt_child ( *offset, tmp, offset );
00263                 if ( del )
00264                         *del = '/';
00265                 if ( rc != 0 )
00266                         return rc;
00267 
00268                 /* Move to next path component, if any */
00269                 while ( *tmp && ( *tmp != '/' ) )
00270                         tmp++;
00271         }
00272 
00273         DBGC2 ( &fdt, "FDT found path \"%s\" at +%#04x\n", path, *offset );
00274         return 0;
00275 }
00276 
00277 /**
00278  * Find node by alias
00279  *
00280  * @v name              Alias name
00281  * @v offset            Offset to fill in
00282  * @ret rc              Return status code
00283  */
00284 int fdt_alias ( const char *name, unsigned int *offset ) {
00285         const char *alias;
00286         int rc;
00287 
00288         /* Locate "/aliases" node */
00289         if ( ( rc = fdt_child ( 0, "aliases", offset ) ) != 0 )
00290                 return rc;
00291 
00292         /* Locate alias property */
00293         if ( ( alias = fdt_string ( *offset, name ) ) == NULL )
00294                 return -ENOENT;
00295         DBGC ( &fdt, "FDT alias \"%s\" is \"%s\"\n", name, alias );
00296 
00297         /* Locate aliased node */
00298         if ( ( rc = fdt_path ( alias, offset ) ) != 0 )
00299                 return rc;
00300 
00301         return 0;
00302 }
00303 
00304 /**
00305  * Find property
00306  *
00307  * @v offset            Starting node offset
00308  * @v name              Property name
00309  * @v desc              Lexical descriptor to fill in
00310  * @ret rc              Return status code
00311  */
00312 static int fdt_property ( unsigned int offset, const char *name,
00313                           struct fdt_descriptor *desc ) {
00314         struct fdt_cursor pos;
00315         int rc;
00316 
00317         /* Initialise cursor */
00318         pos.offset = offset;
00319         pos.depth = -1;
00320 
00321         /* Find property */
00322         while ( 1 ) {
00323 
00324                 /* Traverse tree */
00325                 if ( ( rc = fdt_traverse ( &pos, desc ) ) != 0 ) {
00326                         DBGC ( &fdt, "FDT +%#04x has no property \"%s\": %s\n",
00327                                offset, name, strerror ( rc ) );
00328                         return rc;
00329                 }
00330 
00331                 /* Check for matching immediate child property */
00332                 if ( ( pos.depth == 0 ) && desc->data ) {
00333                         DBGC2 ( &fdt, "FDT +%#04x has property \"%s\" len "
00334                                 "%#zx\n", offset, desc->name, desc->len );
00335                         if ( strcmp ( name, desc->name ) == 0 ) {
00336                                 DBGC2 ( &fdt, "FDT +%#04x found property "
00337                                         "\"%s\"\n", offset, desc->name );
00338                                 DBGC2_HDA ( &fdt, 0, desc->data, desc->len );
00339                                 return 0;
00340                         }
00341                 }
00342         }
00343 }
00344 
00345 /**
00346  * Find string property
00347  *
00348  * @v offset            Starting node offset
00349  * @v name              Property name
00350  * @ret string          String property, or NULL on error
00351  */
00352 const char * fdt_string ( unsigned int offset, const char *name ) {
00353         struct fdt_descriptor desc;
00354         int rc;
00355 
00356         /* Find property */
00357         if ( ( rc = fdt_property ( offset, name, &desc ) ) != 0 )
00358                 return NULL;
00359 
00360         /* Check NUL termination */
00361         if ( strnlen ( desc.data, desc.len ) == desc.len ) {
00362                 DBGC ( &fdt, "FDT unterminated string property \"%s\"\n",
00363                        name );
00364                 return NULL;
00365         }
00366 
00367         return desc.data;
00368 }
00369 
00370 /**
00371  * Get MAC address from property
00372  *
00373  * @v offset            Starting node offset
00374  * @v netdev            Network device
00375  * @ret rc              Return status code
00376  */
00377 int fdt_mac ( unsigned int offset, struct net_device *netdev ) {
00378         struct fdt_descriptor desc;
00379         size_t len;
00380         int rc;
00381 
00382         /* Find applicable MAC address property */
00383         if ( ( ( rc = fdt_property ( offset, "mac-address", &desc ) ) != 0 ) &&
00384              ( ( rc = fdt_property ( offset, "local-mac-address",
00385                                      &desc ) ) != 0 ) ) {
00386                 return rc;
00387         }
00388 
00389         /* Check length */
00390         len = netdev->ll_protocol->hw_addr_len;
00391         if ( len != desc.len ) {
00392                 DBGC ( &fdt, "FDT malformed MAC address \"%s\":\n",
00393                        desc.name );
00394                 DBGC_HDA ( &fdt, 0, desc.data, desc.len );
00395                 return -ERANGE;
00396         }
00397 
00398         /* Fill in MAC address */
00399         memcpy ( netdev->hw_addr, desc.data, len );
00400 
00401         return 0;
00402 }
00403 
00404 /**
00405  * Register device tree
00406  *
00407  * @v fdt               Device tree header
00408  * @ret rc              Return status code
00409  */
00410 int register_fdt ( const struct fdt_header *hdr ) {
00411         const uint8_t *end;
00412 
00413         /* Record device tree location */
00414         fdt.hdr = hdr;
00415         fdt.len = be32_to_cpu ( hdr->totalsize );
00416         DBGC ( &fdt, "FDT version %d at %p+%#04zx\n",
00417                be32_to_cpu ( hdr->version ), fdt.hdr, fdt.len );
00418 
00419         /* Check signature */
00420         if ( hdr->magic != cpu_to_be32 ( FDT_MAGIC ) ) {
00421                 DBGC ( &fdt, "FDT has invalid magic value %#08x\n",
00422                        be32_to_cpu ( hdr->magic ) );
00423                 goto err;
00424         }
00425 
00426         /* Check version */
00427         if ( hdr->last_comp_version != cpu_to_be32 ( FDT_VERSION ) ) {
00428                 DBGC ( &fdt, "FDT unsupported version %d\n",
00429                        be32_to_cpu ( hdr->last_comp_version ) );
00430                 goto err;
00431         }
00432 
00433         /* Record structure block location */
00434         fdt.structure = be32_to_cpu ( hdr->off_dt_struct );
00435         fdt.structure_len = be32_to_cpu ( hdr->size_dt_struct );
00436         DBGC ( &fdt, "FDT structure block at +[%#04x,%#04zx)\n",
00437                fdt.structure, ( fdt.structure + fdt.structure_len ) );
00438         if ( ( fdt.structure > fdt.len ) ||
00439              ( fdt.structure_len > ( fdt.len - fdt.structure ) ) ) {
00440                 DBGC ( &fdt, "FDT structure block exceeds table\n" );
00441                 goto err;
00442         }
00443         if ( ( fdt.structure | fdt.structure_len ) &
00444              ( FDT_STRUCTURE_ALIGN - 1 ) ) {
00445                 DBGC ( &fdt, "FDT structure block is misaligned\n" );
00446                 goto err;
00447         }
00448 
00449         /* Record strings block location */
00450         fdt.strings = be32_to_cpu ( hdr->off_dt_strings );
00451         fdt.strings_len = be32_to_cpu ( hdr->size_dt_strings );
00452         DBGC ( &fdt, "FDT strings block at +[%#04x,%#04zx)\n",
00453                fdt.strings, ( fdt.strings + fdt.strings_len ) );
00454         if ( ( fdt.strings > fdt.len ) ||
00455              ( fdt.strings_len > ( fdt.len - fdt.strings ) ) ) {
00456                 DBGC ( &fdt, "FDT strings block exceeds table\n" );
00457                 goto err;
00458         }
00459 
00460         /* Shrink strings block to ensure NUL termination safety */
00461         end = ( fdt.raw + fdt.strings + fdt.strings_len );
00462         for ( ; fdt.strings_len ; fdt.strings_len-- ) {
00463                 if ( *(--end) == '\0' )
00464                         break;
00465         }
00466         if ( fdt.strings_len != be32_to_cpu ( hdr->size_dt_strings ) ) {
00467                 DBGC ( &fdt, "FDT strings block shrunk to +[%#04x,%#04zx)\n",
00468                        fdt.strings, ( fdt.strings + fdt.strings_len ) );
00469         }
00470 
00471         /* Print model name (for debugging) */
00472         DBGC ( &fdt, "FDT model is \"%s\"\n", fdt_string ( 0, "model" ) );
00473 
00474         return 0;
00475 
00476  err:
00477         DBGC_HDA ( &fdt, 0, hdr, sizeof ( *hdr ) );
00478         fdt.hdr = NULL;
00479         return -EINVAL;
00480 }
00481 
00482 /* Drag in objects via register_fdt */
00483 REQUIRING_SYMBOL ( register_fdt );
00484 
00485 /* Drag in device tree configuration */
00486 REQUIRE_OBJECT ( config_fdt );