| 1146 | } |
| 1147 | |
| 1148 | fn print_node(node: fdt_parser::node::FdtNode<'_, '_>, n_spaces: usize) { |
| 1149 | debug!("{:indent$}{}/", "", node.name, indent = n_spaces); |
| 1150 | for property in node.properties() { |
| 1151 | let name = property.name; |
| 1152 | |
| 1153 | // If the property is 'compatible', its value requires special handling. |
| 1154 | // The u8 array could contain multiple null-terminated strings. |
| 1155 | // We copy the original array and simply replace all 'null' characters with spaces. |
| 1156 | let value = if name == "compatible" { |
| 1157 | let mut compatible = vec![0u8; 256]; |
| 1158 | let handled_value = property |
| 1159 | .value |
| 1160 | .iter() |
| 1161 | .map(|&c| if c == 0 { b' ' } else { c }) |
| 1162 | .collect::<Vec<_>>(); |
| 1163 | let len = cmp::min(255, handled_value.len()); |
| 1164 | compatible[..len].copy_from_slice(&handled_value[..len]); |
| 1165 | compatible[..(len + 1)].to_vec() |
| 1166 | } else { |
| 1167 | property.value.to_vec() |
| 1168 | }; |
| 1169 | let value = &value; |
| 1170 | |
| 1171 | // Now the value can be either: |
| 1172 | // - A null-terminated C string, or |
| 1173 | // - Binary data |
| 1174 | // We follow a very simple logic to present the value: |
| 1175 | // - At first, try to convert it to CStr and print, |
| 1176 | // - If failed, print it as u32 array. |
| 1177 | let value_result = match CStr::from_bytes_with_nul(value) { |
| 1178 | Ok(value_cstr) => value_cstr.to_str().ok(), |
| 1179 | Err(_e) => None, |
| 1180 | }; |
| 1181 | |
| 1182 | if let Some(value_str) = value_result { |
| 1183 | debug!( |
| 1184 | "{:indent$}{} : {:#?}", |
| 1185 | "", |
| 1186 | name, |
| 1187 | value_str, |
| 1188 | indent = (n_spaces + 2) |
| 1189 | ); |
| 1190 | } else { |
| 1191 | let mut array = Vec::with_capacity(256); |
| 1192 | array.resize(value.len() / 4, 0u32); |
| 1193 | BigEndian::read_u32_into(value, &mut array); |
| 1194 | debug!( |
| 1195 | "{:indent$}{} : {:X?}", |
| 1196 | "", |
| 1197 | name, |
| 1198 | array, |
| 1199 | indent = (n_spaces + 2) |
| 1200 | ); |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | // Print children nodes if there is any |
| 1205 | for child in node.children() { |