blob: a682dbc3449d8c0f9322facbdac0f3b1aff0f9d9 [file] [log] [blame]
Sol Boucher69b88bf2015-02-26 11:47:19 -08001/*
2 * fmd_parser.y, parser generator for flashmap descriptor language
3 *
4 * Copyright (C) 2015 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Sol Boucher69b88bf2015-02-26 11:47:19 -080014 */
15
16%{
17#include "fmd_scanner.h"
Sol Boucherb9740812015-03-18 10:13:48 -070018#include "common.h"
Sol Boucher69b88bf2015-02-26 11:47:19 -080019
20#include <stdlib.h>
21
22struct flashmap_descriptor *res = NULL;
23%}
24
25%union {
26 unsigned intval;
27 char *strval;
28 struct unsigned_option maybe_intval;
29 struct flashmap_descriptor *region_ptr;
30 struct descriptor_list region_listhdr;
31}
32
33%code requires {
34#include "fmd.h"
35#include "option.h"
36
37#include <stdbool.h>
38
39struct descriptor_node {
40 struct flashmap_descriptor *val;
41 struct descriptor_node *next;
42};
43
44struct descriptor_list {
45 size_t len;
46 struct descriptor_node *head;
47 struct descriptor_node *tail;
48};
49
50extern struct flashmap_descriptor *res;
51
52struct flashmap_descriptor *parse_descriptor(char *name,
53 struct unsigned_option offset, struct unsigned_option size,
54 struct descriptor_list children);
55void yyerror(const char *s);
56}
57
58%token <intval> INTEGER
59%token OCTAL
60%token <strval> STRING
61
62%type <region_ptr> flash_region
63%type <strval> region_name
64%type <strval> region_annotation_opt
65%type <strval> region_annotation
66%type <maybe_intval> region_offset_opt
67%type <maybe_intval> region_offset
68%type <maybe_intval> region_size_opt
69%type <maybe_intval> region_size
70%type <region_listhdr> region_list_opt
71%type <region_listhdr> region_list
72%type <region_listhdr> region_list_entries
73
74%%
75
76flash_chip: region_name region_offset_opt region_size region_list
77{
78 if (!(res = parse_descriptor($1, $2, $3, $4)))
79 YYABORT;
80};
81flash_region: region_name region_annotation_opt region_offset_opt
82 region_size_opt region_list_opt
83{
84 struct flashmap_descriptor *node = parse_descriptor($1, $3, $4, $5);
85 if (!node)
86 YYABORT;
87
88 char *annotation = $2;
89 if (annotation && !fmd_process_annotation_impl(node, annotation)) {
Sol Boucherb9740812015-03-18 10:13:48 -070090 ERROR("Section '%s' has unexpected annotation '(%s)'\n",
Sol Boucher69b88bf2015-02-26 11:47:19 -080091 node->name, annotation);
92 YYABORT;
93 }
94 free(annotation);
95
96 $$ = node;
97};
98region_name: STRING
99{
100 if (!$1) {
Sol Boucherb9740812015-03-18 10:13:48 -0700101 perror("E: While allocating section name");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800102 YYABORT;
103 }
104};
105region_annotation_opt: { $$ = NULL; }
106 | region_annotation;
107region_annotation: '(' STRING ')' { $$ = $2; };
108region_offset_opt: { $$ = (struct unsigned_option){false, 0}; }
109 | region_offset;
110region_offset: '@' INTEGER { $$ = (struct unsigned_option){true, $2}; };
111region_size_opt: { $$ = (struct unsigned_option){false, 0}; }
112 | region_size;
113region_size: INTEGER { $$ = (struct unsigned_option){true, $1}; };
114region_list_opt:
115{
116 $$ = (struct descriptor_list)
117 {.len = 0, .head = NULL, .tail = NULL};
118}
119 | region_list;
120region_list: '{' region_list_entries '}' { $$ = $2; };
121region_list_entries: flash_region
122{
123 struct descriptor_node *node = malloc(sizeof(*node));
124 if (!node) {
Sol Boucherb9740812015-03-18 10:13:48 -0700125 perror("E: While allocating linked list node");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800126 YYABORT;
127 }
128 node->val = $1;
129 node->next = NULL;
130 $$ = (struct descriptor_list){.len = 1, .head = node, .tail = node};
131}
132 | region_list_entries flash_region
133{
134 struct descriptor_node *node = malloc(sizeof(*node));
135 if (!node) {
Sol Boucherb9740812015-03-18 10:13:48 -0700136 perror("E: While allocating linked list node");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800137 YYABORT;
138 }
139 node->val = $2;
140 node->next = NULL;
141
142 $1.tail->next = node;
143 $$ = (struct descriptor_list)
144 {.len = $1.len + 1, .head = $1.head, .tail = node};
145};
146
147%%
148
149struct flashmap_descriptor *parse_descriptor(char *name,
150 struct unsigned_option offset, struct unsigned_option size,
151 struct descriptor_list children)
152{
153 struct flashmap_descriptor *region = malloc(sizeof(*region));
154 if (!region) {
Sol Boucherb9740812015-03-18 10:13:48 -0700155 perror("E: While allocating descriptor section");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800156 return NULL;
157 }
158 region->name = name;
159 region->offset_known = offset.val_known;
160 region->offset = offset.val;
161 region->size_known = size.val_known;
162 region->size = size.val;
163 region->list_len = children.len;
164 if (region->list_len) {
165 region->list = malloc(region->list_len * sizeof(*region->list));
166 if (!region->list) {
Sol Boucherb9740812015-03-18 10:13:48 -0700167 perror("E: While allocating node children array");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800168 return NULL;
169 }
170 struct descriptor_node *cur_node = children.head;
171 for (unsigned idx = 0; idx < region->list_len; ++idx) {
172 region->list[idx] = cur_node->val;
173
174 struct descriptor_node *next_node = cur_node->next;
175 free(cur_node);
176 cur_node = next_node;
177 }
178 } else {
179 region->list = NULL;
180 }
181 return region;
182}
183
184void yyerror(const char *s)
185{
186 fprintf(stderr, "%s\n", s);
187}