header.c 858 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "../../util/header.h"
  7. #define __stringify_1(x) #x
  8. #define __stringify(x) __stringify_1(x)
  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. }