test.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * test.c
  3. *
  4. * Copyright (C) 2023 bzt
  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; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * @brief Hardware Resource Detection single header library test app
  21. *
  22. * Compilation: gcc test.c -o test
  23. */
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. /* the two hooks you provide */
  29. void mem_exclude(uint64_t base, uint64_t size) {
  30. printf("unusable memory %lx %ld\n", base, size);
  31. }
  32. void drv_resource(char *name, char *driver, char *altdriver, int type, uint64_t arg1, uint64_t arg2)
  33. {
  34. char *types[] = { "none ", "CPU ", "IOport", "IRQ ", "DMA ", "MMIO ", "PCI ", "EC ", "SMB ", "CMOS " };
  35. printf("device '%s' driver '%s' (alt '%s') type %d %s arguments %lx %ld\n", name, driver, altdriver,
  36. type, type < (int)(sizeof(types)/sizeof(types[0])) ? types[type] : "???", arg1, arg2);
  37. }
  38. /* include the library */
  39. #define HWDET_RESVMEM mem_exclude
  40. #define HWDET_RESOURCE drv_resource
  41. #include "hwdet.h"
  42. /* a minimal example that reads tables from files (you probably want to locate ACPI tables in memory and pass addresses of those) */
  43. int main(int argc, char *argv[])
  44. {
  45. FILE *f;
  46. uint8_t *ds[16] = { 0 };
  47. int size, i, num = 0;
  48. /* parse arguments */
  49. if(argc < 2) {
  50. printf("HwDet, Copyright (c) 2023 bzt, GPLv3+\nhttps://gitlab.com/bztsrc/hwdet\n\n");
  51. printf("%s file [file [file [...]]]\n\n", argv[0]);
  52. printf("Files can be ACPI DSDT/SSDT tables, FDT (dtb) or GUDT blobs.\n");
  53. return 1;
  54. }
  55. for(i = 1; i < argc && i < 16; i++) {
  56. f = fopen(argv[i], "rb");
  57. if(f) {
  58. fseek(f, 0, SEEK_END);
  59. size = (int)ftell(f);
  60. fseek(f, 0, SEEK_SET);
  61. ds[num] = malloc(size);
  62. if(ds[num]) size = fread(ds[num++], 1, size, f);
  63. fclose(f);
  64. }
  65. }
  66. /* call the library */
  67. hwdet(num, ds);
  68. /* free resources */
  69. for(i = 0; i < num; i++) if(ds[i]) free(ds[i]);
  70. printf("Done.\n");
  71. return 0;
  72. }