header.c 936 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <linux/stringify.h>
  7. #include "header.h"
  8. #include "util.h"
  9. #define mfspr(rn) ({unsigned long rval; \
  10. asm volatile("mfspr %0," __stringify(rn) \
  11. : "=r" (rval)); rval; })
  12. #define SPRN_PVR 0x11F /* Processor Version Register */
  13. #define PVR_VER(pvr) (((pvr) >> 16) & 0xFFFF) /* Version field */
  14. #define PVR_REV(pvr) (((pvr) >> 0) & 0xFFFF) /* Revison field */
  15. int
  16. get_cpuid(char *buffer, size_t sz)
  17. {
  18. unsigned long pvr;
  19. int nb;
  20. pvr = mfspr(SPRN_PVR);
  21. nb = scnprintf(buffer, sz, "%lu,%lu$", PVR_VER(pvr), PVR_REV(pvr));
  22. /* look for end marker to ensure the entire data fit */
  23. if (strchr(buffer, '$')) {
  24. buffer[nb-1] = '\0';
  25. return 0;
  26. }
  27. return -1;
  28. }
  29. char *
  30. get_cpuid_str(void)
  31. {
  32. char *bufp;
  33. if (asprintf(&bufp, "%.8lx", mfspr(SPRN_PVR)) < 0)
  34. bufp = NULL;
  35. return bufp;
  36. }