EmuProxyZA / bootpage.c

From TivoZA

Back to emuProxyZA.
Download source (You will need to rename it to: bootpage.c)

To update the below source file, select Upload file and upload your new bootpage.c file (there is no need to upload it as a .txt). If you do upload a new version, please add a brief description to the change log at the bottom of this page indicating what changes you made and why.

Source

  1. /*
  2. * bootpage.c is a modified version of:
  3. * $Id: tpip.c,v 1.19 2004/01/12 03:12:34 millert Exp $
  4. *
  5. * Copyright (c) 2003-2004 Todd C. Miller <Todd.Miller@courtesan.com>
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19.  
  20. #include <fcntl.h>
  21.  
  22. #if BYTE_ORDER == BIG_ENDIAN
  23. # define TIVO_BOOT_MAGIC 0x1492
  24. # define MAC_PARTITION_MAGIC 0x504d
  25. #elif BYTE_ORDER == LITTLE_ENDIAN
  26. # define TIVO_BOOT_MAGIC 0x9214
  27. # define MAC_PARTITION_MAGIC 0x4d50
  28. #else
  29. # error Unknown byte order!
  30. #endif
  31. #define HOST_BYTE_ORDER BYTE_ORDER
  32.  
  33. struct tcdbootpage {
  34. /*
  35. * NOTE: values are stored in network byte order (little endian)
  36. */
  37. u_int16_t signature;
  38. u_int8_t primaryBootPartition;
  39. u_int8_t alternateBootPartition;
  40. char bootParams[128];
  41. char padding[380]; /* we don't need the other bits */
  42. };
  43.  
  44. int TIVO_BYTE_ORDER;
  45.  
  46. char *bootpage(const char *device) {
  47. int fd;
  48. struct tcdbootpage *bootpg, bpbuf[2];
  49.  
  50. if ((fd = open(device, O_RDWR, 0644)) == -1)
  51. err(1, "error opening %s", device);
  52.  
  53. /* read the TiVo boot page */
  54. bootpg = &bpbuf[0];
  55. switch (read(fd, bootpg, sizeof(struct tcdbootpage))) {
  56. case -1:
  57. fprintf(stderr, "error reading from %s", device);
  58. return NULL;
  59. case sizeof(struct tcdbootpage):
  60. /* OK */
  61. break;
  62. default:
  63. fprintf(stderr, "%s: short read", device);
  64. return NULL;
  65. }
  66.  
  67. if (bootpg->signature == TIVO_BOOT_MAGIC) {
  68. TIVO_BYTE_ORDER = HOST_BYTE_ORDER;
  69. } else if (bootpg->signature == cpu_to_be16(TIVO_BOOT_MAGIC)) {
  70. // TiVo disk is byte-swapped
  71. #if HOST_BYTE_ORDER == BIG_ENDIAN
  72. TIVO_BYTE_ORDER = LITTLE_ENDIAN;
  73. #else
  74. TIVO_BYTE_ORDER = BIG_ENDIAN;
  75. #endif
  76. swab((char *)&bpbuf[0], (char *)&bpbuf[1],
  77. sizeof(struct tcdbootpage));
  78. bootpg = &bpbuf[1];
  79. } else if (bootpg->signature != TIVO_BOOT_MAGIC) {
  80. fprintf(stderr, "unknown bootpage signature: 0x%x", bootpg->signature);
  81. return NULL;
  82. }
  83.  
  84. fsync(fd);
  85.  
  86. return bootpg->bootParams;
  87. }

Change Log

20 Oct 2006 - TivoZA: Uploaded the file

Advertisement