1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <ofapi/of_api.h>
uint64_t total_ram;
uint32_t cells_addr;
uint32_t cells_size;
void calculate_ram(uint8_t * ptr, uint32_t size)
{
int i = 1;
int num = 0;
int status = 1;
uint64_t size64;
uint32_t size32;
uint8_t *p = ptr;
while(status) {
if((cells_addr == 2) && (cells_size == 1))
i+=2;
else
i++;
if(cells_size == 2) {
status=of_property_to_n_uint64(&size64, p, size, i++);
if(!status)
break;
total_ram+=size64;
} else {
status=of_property_to_n_uint32(&size32, p, size, i++);
if(!status)
break;
total_ram+=size32;
}
}
}
int main(int argc, char **argv)
{
struct device_node *node;
struct device_node *parent;
int plen = 0;
void *property;
if(argc == 1)
of_init();
else
of_init_root(argv[1]);
while ((node = of_find_node_by_name("memory", 1)) != NULL) {
if((parent=of_get_parent(node))) {
if((property = of_find_property(parent,"#address-cells", &plen))) {
of_property_to_uint32(&cells_addr, property, plen);
free(property);
}
if((property = of_find_property(parent,"#size-cells", &plen))) {
of_property_to_uint32(&cells_size, property, plen);
free(property);
}
property = of_find_property(node, "reg", &plen);
calculate_ram(property, plen);
free(property);
of_free_node(parent);
}
of_free_node(node);
}
printf("Total bytes %lld\n", total_ram);
printf("Total MB %lld\n", total_ram >> 20);
printf("Total GB %lld\n", total_ram >> 30);
return EXIT_SUCCESS;
}
|