apm.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*-
  2. * Copyright (c) 2014 Juniper Networks, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. */
  26. #include <sys/cdefs.h>
  27. #include <sys/errno.h>
  28. #include <stdint.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <apm.h>
  32. #include "endian.h"
  33. #include "image.h"
  34. #include "mkimg.h"
  35. #include "scheme.h"
  36. static struct mkimg_alias apm_aliases[] = {
  37. { ALIAS_FREEBSD, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD) },
  38. { ALIAS_FREEBSD_BOOT, ALIAS_PTR2TYPE(APM_ENT_TYPE_APPLE_BOOT) },
  39. { ALIAS_FREEBSD_NANDFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_NANDFS) },
  40. { ALIAS_FREEBSD_SWAP, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_SWAP) },
  41. { ALIAS_FREEBSD_UFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_UFS) },
  42. { ALIAS_FREEBSD_VINUM, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_VINUM) },
  43. { ALIAS_FREEBSD_ZFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_ZFS) },
  44. { ALIAS_NONE, 0 }
  45. };
  46. static lba_t
  47. apm_metadata(u_int where, lba_t blk)
  48. {
  49. blk += (where == SCHEME_META_IMG_START) ? nparts + 2 : 0;
  50. return (round_block(blk));
  51. }
  52. static int
  53. apm_write(lba_t imgsz, void *bootcode __unused)
  54. {
  55. u_char *buf;
  56. struct apm_ddr *ddr;
  57. struct apm_ent *ent;
  58. struct part *part;
  59. int error;
  60. buf = calloc(nparts + 2, secsz);
  61. if (buf == NULL)
  62. return (ENOMEM);
  63. ddr = (void *)buf;
  64. be16enc(&ddr->ddr_sig, APM_DDR_SIG);
  65. be16enc(&ddr->ddr_blksize, secsz);
  66. be32enc(&ddr->ddr_blkcount, imgsz);
  67. /* partition entry for the partition table itself. */
  68. ent = (void *)(buf + secsz);
  69. be16enc(&ent->ent_sig, APM_ENT_SIG);
  70. be32enc(&ent->ent_pmblkcnt, nparts + 1);
  71. be32enc(&ent->ent_start, 1);
  72. be32enc(&ent->ent_size, nparts + 1);
  73. strncpy(ent->ent_type, APM_ENT_TYPE_SELF, sizeof(ent->ent_type));
  74. strncpy(ent->ent_name, "Apple", sizeof(ent->ent_name));
  75. TAILQ_FOREACH(part, &partlist, link) {
  76. ent = (void *)(buf + (part->index + 2) * secsz);
  77. be16enc(&ent->ent_sig, APM_ENT_SIG);
  78. be32enc(&ent->ent_pmblkcnt, nparts + 1);
  79. be32enc(&ent->ent_start, part->block);
  80. be32enc(&ent->ent_size, part->size);
  81. strncpy(ent->ent_type, ALIAS_TYPE2PTR(part->type),
  82. sizeof(ent->ent_type));
  83. if (part->label != NULL)
  84. strncpy(ent->ent_name, part->label,
  85. sizeof(ent->ent_name));
  86. }
  87. error = image_write(0, buf, nparts + 2);
  88. free(buf);
  89. return (error);
  90. }
  91. static struct mkimg_scheme apm_scheme = {
  92. .name = "apm",
  93. .description = "Apple Partition Map",
  94. .aliases = apm_aliases,
  95. .metadata = apm_metadata,
  96. .write = apm_write,
  97. .nparts = 4096,
  98. .labellen = APM_ENT_NAMELEN - 1,
  99. .maxsecsz = 4096
  100. };
  101. SCHEME_DEFINE(apm_scheme);