Browse Source

dtc: Support character literals in cell lists

With this patch the following property assignment:

    property = <0x12345678 'a' '\r' 100>;

is equivalent to:

    property = <0x12345678 0x00000061 0x0000000D 0x00000064>

Signed-off-by: Anton Staaf <robotboy@chromium.org>
Acked-by: David Gibson <david@gibson.dropbear.id.au>
main
Anton Staaf 13 years ago committed by Jon Loeliger
parent
commit
a4ea2fa951
  1. 2
      Documentation/dts-format.txt
  2. 8
      dtc-lexer.l
  3. 32
      dtc-parser.y
  4. 1
      tests/.gitignore
  5. 1
      tests/Makefile.tests
  6. 50
      tests/char_literal.c
  7. 5
      tests/char_literal.dts
  8. 3
      tests/run_tests.sh
  9. 6
      tests/testdata.h

2
Documentation/dts-format.txt

@ -33,7 +33,7 @@ Property values may be defined as an array of 32-bit integer cells, as
NUL-terminated strings, as bytestrings or a combination of these. NUL-terminated strings, as bytestrings or a combination of these.


* Arrays of cells are represented by angle brackets surrounding a * Arrays of cells are represented by angle brackets surrounding a
space separated list of C-style integers space separated list of C-style integers or character literals.


e.g. interrupts = <17 0xc>; e.g. interrupts = <17 0xc>;



8
dtc-lexer.l

@ -29,6 +29,7 @@ PROPNODECHAR [a-zA-Z0-9,._+*#?@-]
PATHCHAR ({PROPNODECHAR}|[/]) PATHCHAR ({PROPNODECHAR}|[/])
LABEL [a-zA-Z_][a-zA-Z0-9_]* LABEL [a-zA-Z_][a-zA-Z0-9_]*
STRING \"([^\\"]|\\.)*\" STRING \"([^\\"]|\\.)*\"
CHAR_LITERAL '([^']|\\')*'
WS [[:space:]] WS [[:space:]]
COMMENT "/*"([^*]|\*+[^*/])*\*+"/" COMMENT "/*"([^*]|\*+[^*/])*\*+"/"
LINECOMMENT "//".*\n LINECOMMENT "//".*\n
@ -109,6 +110,13 @@ static int pop_input_file(void);
return DT_LITERAL; return DT_LITERAL;
} }


<*>{CHAR_LITERAL} {
yytext[yyleng-1] = '\0';
yylval.literal = xstrdup(yytext+1);
DPRINT("Character literal: %s\n", yylval.literal);
return DT_CHAR_LITERAL;
}

<*>\&{LABEL} { /* label reference */ <*>\&{LABEL} { /* label reference */
DPRINT("Ref: %s\n", yytext+1); DPRINT("Ref: %s\n", yytext+1);
yylval.labelref = xstrdup(yytext+1); yylval.labelref = xstrdup(yytext+1);

32
dtc-parser.y

@ -34,6 +34,7 @@ extern struct boot_info *the_boot_info;
extern int treesource_error; extern int treesource_error;


static unsigned long long eval_literal(const char *s, int base, int bits); static unsigned long long eval_literal(const char *s, int base, int bits);
static unsigned char eval_char_literal(const char *s);
%} %}


%union { %union {
@ -57,6 +58,7 @@ static unsigned long long eval_literal(const char *s, int base, int bits);
%token DT_MEMRESERVE %token DT_MEMRESERVE
%token <propnodename> DT_PROPNODENAME %token <propnodename> DT_PROPNODENAME
%token <literal> DT_LITERAL %token <literal> DT_LITERAL
%token <literal> DT_CHAR_LITERAL
%token <cbase> DT_BASE %token <cbase> DT_BASE
%token <byte> DT_BYTE %token <byte> DT_BYTE
%token <data> DT_STRING %token <data> DT_STRING
@ -265,6 +267,10 @@ cellval:
{ {
$$ = eval_literal($1, 0, 32); $$ = eval_literal($1, 0, 32);
} }
| DT_CHAR_LITERAL
{
$$ = eval_char_literal($1);
}
; ;


bytestring: bytestring:
@ -343,3 +349,29 @@ static unsigned long long eval_literal(const char *s, int base, int bits)
print_error("bad literal"); print_error("bad literal");
return val; return val;
} }

static unsigned char eval_char_literal(const char *s)
{
int i = 1;
char c = s[0];

if (c == '\0')
{
print_error("empty character literal");
return 0;
}

/*
* If the first character in the character literal is a \ then process
* the remaining characters as an escape encoding. If the first
* character is neither an escape or a terminator it should be the only
* character in the literal and will be returned.
*/
if (c == '\\')
c = get_escape_char(s, &i);

if (s[i] != '\0')
print_error("malformed character literal");

return c;
}

1
tests/.gitignore vendored

@ -4,6 +4,7 @@
/add_subnode_with_nops /add_subnode_with_nops
/asm_tree_dump /asm_tree_dump
/boot-cpuid /boot-cpuid
/char_literal
/del_node /del_node
/del_property /del_property
/dtbs_equal_ordered /dtbs_equal_ordered

1
tests/Makefile.tests

@ -5,6 +5,7 @@ LIB_TESTS_L = get_mem_rsv \
node_offset_by_prop_value node_offset_by_phandle \ node_offset_by_prop_value node_offset_by_phandle \
node_check_compatible node_offset_by_compatible \ node_check_compatible node_offset_by_compatible \
get_alias \ get_alias \
char_literal \
notfound \ notfound \
setprop_inplace nop_property nop_node \ setprop_inplace nop_property nop_node \
sw_tree1 \ sw_tree1 \

50
tests/char_literal.c

@ -0,0 +1,50 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for character literals in dtc
* Copyright (C) 2006 David Gibson, IBM Corporation.
* Copyright (C) 2011 The Chromium Authors. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include <fdt.h>
#include <libfdt.h>

#include "tests.h"
#include "testdata.h"

int main(int argc, char *argv[])
{
void *fdt;
uint32_t expected_cells[5];

expected_cells[0] = cpu_to_fdt32((unsigned char)TEST_CHAR1);
expected_cells[1] = cpu_to_fdt32((unsigned char)TEST_CHAR2);
expected_cells[2] = cpu_to_fdt32((unsigned char)TEST_CHAR3);
expected_cells[3] = cpu_to_fdt32((unsigned char)TEST_CHAR4);
expected_cells[4] = cpu_to_fdt32((unsigned char)TEST_CHAR5);

test_init(argc, argv);
fdt = load_blob_arg(argc, argv);

check_getprop(fdt, 0, "char-literal-cells",
sizeof(expected_cells), expected_cells);

PASS();
}

5
tests/char_literal.dts

@ -0,0 +1,5 @@
/dts-v1/;

/ {
char-literal-cells = <'\r' 'b' '\0' '\'' '\xff'>;
};

3
tests/run_tests.sh

@ -206,6 +206,9 @@ dtc_tests () {
run_dtc_test -I dts -O dtb -o dtc_escapes.test.dtb escapes.dts run_dtc_test -I dts -O dtb -o dtc_escapes.test.dtb escapes.dts
run_test string_escapes dtc_escapes.test.dtb run_test string_escapes dtc_escapes.test.dtb


run_dtc_test -I dts -O dtb -o dtc_char_literal.test.dtb char_literal.dts
run_test char_literal dtc_char_literal.test.dtb

run_dtc_test -I dts -O dtb -o dtc_extra-terminating-null.test.dtb extra-terminating-null.dts run_dtc_test -I dts -O dtb -o dtc_extra-terminating-null.test.dtb extra-terminating-null.dts
run_test extra-terminating-null dtc_extra-terminating-null.test.dtb run_test extra-terminating-null dtc_extra-terminating-null.test.dtb



6
tests/testdata.h

@ -19,6 +19,12 @@
#define TEST_STRING_2 "nastystring: \a\b\t\n\v\f\r\\\"" #define TEST_STRING_2 "nastystring: \a\b\t\n\v\f\r\\\""
#define TEST_STRING_3 "\xde\xad\xbe\xef" #define TEST_STRING_3 "\xde\xad\xbe\xef"


#define TEST_CHAR1 '\r'
#define TEST_CHAR2 'b'
#define TEST_CHAR3 '\0'
#define TEST_CHAR4 '\''
#define TEST_CHAR5 '\xff'

#ifndef __ASSEMBLY__ #ifndef __ASSEMBLY__
extern struct fdt_header _test_tree1; extern struct fdt_header _test_tree1;
extern struct fdt_header _truncated_property; extern struct fdt_header _truncated_property;

Loading…
Cancel
Save