Skip to content

fix(grub): Replace DeepinGfxmodeNotSupported mechanism with EDID hash caching#1043

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
electricface:swt/grub-gfxmode-detect
Mar 4, 2026
Merged

fix(grub): Replace DeepinGfxmodeNotSupported mechanism with EDID hash caching#1043
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
electricface:swt/grub-gfxmode-detect

Conversation

@electricface
Copy link
Member

  • Deprecate DeepinGfxmodeNotSupported key: Removed from the parameter
    initialization list, allowedKeys, and finishGfxmodeDetect. It is
    now unconditionally deleted.
  • Add GetConnectedEdidsHash function (grub_common): Iterates
    through connected monitors' EDIDs and calculates an MD5 hash to
    detect changes in monitor connection status.
  • Add grub_gfx/cache.go: Defines DetectCache (containing EdidsHash
  • MaxGfxmode) and provides loadDetectCache / saveDetectCache to
    persist detection results.
  • Integrate EDID hash comparison in detectChange: Compares the
    EDID hash against the cache; marks detectCacheChanged=true only
    when the hash or maxGfxmode changes.
  • Update needDetect: Replaces the original maxNotSupported parameter
    with detectCacheChanged. The logic is now: config parsing failed
    OR config mismatch OR (current resolution is not max && cache
    has changed).
  • Enhance logging: Logs GRUB params, EDID hash, needDetect
    conditions, and the final result to facilitate debugging.

fix(grub): 用EDID哈希缓存替代DeepinGfxmodeNotSupported机制

  • 废弃 DeepinGfxmodeNotSupported 键:从参数初始化列表、allowedKeys
    及 finishGfxmodeDetect 中移除,改为无条件 delete
  • 新增 GetConnectedEdidsHash 函数(grub_common),遍历已连接显示器
    的 EDID 并计算 MD5 哈希,用于感知显示器连接状态变化
  • 新增 grub_gfx/cache.go:定义 DetectCache(EdidsHash + MaxGfxmode),
    提供 loadDetectCache / saveDetectCache,持久化检测结果
  • 在 detectChange 中引入 EDID 哈希与缓存比较,仅在哈希或
    maxGfxmode 变化时才标记 detectCacheChanged=true
  • needDetect 以 detectCacheChanged 替换原 maxNotSupported 参数,
    逻辑改为:配置解析失败 || 配置与当前不一致 ||
    (当前非最大分辨率 && 缓存已变化)
  • 加强日志记录:打印 grub params、EDID 哈希、needDetect 各条件
    及最终结果,便于调试

Influence: GRUB主题
PMS: BUG-277733, BUG-291909

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @electricface, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: electricface

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

hash caching

- Deprecate DeepinGfxmodeNotSupported key: Removed from the parameter
initialization list, allowedKeys, and finishGfxmodeDetect. It is
now unconditionally deleted.
- Add GetConnectedEdidsHash function (grub_common): Iterates
through connected monitors' EDIDs and calculates an MD5 hash to
detect changes in monitor connection status.
- Add grub_gfx/cache.go: Defines DetectCache (containing EdidsHash
+ MaxGfxmode) and provides loadDetectCache / saveDetectCache to
persist detection results.
- Integrate EDID hash comparison in detectChange: Compares the
EDID hash against the cache; marks detectCacheChanged=true only
when the hash or maxGfxmode changes.
- Update needDetect: Replaces the original maxNotSupported parameter
with detectCacheChanged. The logic is now: config parsing failed
OR config mismatch OR (current resolution is not max && cache
has changed).
- Enhance logging: Logs GRUB params, EDID hash, needDetect
conditions, and the final result to facilitate debugging.

---

fix(grub): 用EDID哈希缓存替代DeepinGfxmodeNotSupported机制

- 废弃 DeepinGfxmodeNotSupported 键:从参数初始化列表、allowedKeys
  及 finishGfxmodeDetect 中移除,改为无条件 delete
- 新增 GetConnectedEdidsHash 函数(grub_common),遍历已连接显示器
  的 EDID 并计算 MD5 哈希,用于感知显示器连接状态变化
- 新增 grub_gfx/cache.go:定义 DetectCache(EdidsHash + MaxGfxmode),
  提供 loadDetectCache / saveDetectCache,持久化检测结果
- 在 detectChange 中引入 EDID 哈希与缓存比较,仅在哈希或
  maxGfxmode 变化时才标记 detectCacheChanged=true
- needDetect 以 detectCacheChanged 替换原 maxNotSupported 参数,
  逻辑改为:配置解析失败 || 配置与当前不一致 ||
  (当前非最大分辨率 && 缓存已变化)
- 加强日志记录:打印 grub params、EDID 哈希、needDetect 各条件
  及最终结果,便于调试

Influence: GRUB主题
PMS: BUG-277733, BUG-291909
@electricface electricface force-pushed the swt/grub-gfxmode-detect branch from d367cbb to ad86888 Compare March 4, 2026 04:30
@deepin-ci-robot
Copy link

deepin pr auto review

代码审查意见

整体评价

这次提交引入了一个新的缓存机制来优化GRUB图形模式检测过程,通过使用EDID哈希值来避免重复检测。整体代码结构清晰,但存在一些安全性和性能方面的改进空间。

具体问题与建议

1. 安全性问题

1.1 MD5哈希算法安全性不足

grub_common/common.goGetConnectedEdidsHash函数中使用了MD5哈希算法:

h := md5.New()

问题:MD5已被证明存在碰撞攻击风险,不适合用于安全敏感场景。虽然这里用于缓存键值可能不会直接导致安全问题,但建议使用更安全的哈希算法。

建议:改用SHA-256或SHA-3等更安全的哈希算法:

import "crypto/sha256"

// 在GetConnectedEdidsHash函数中替换
h := sha256.New()

1.2 文件权限设置

grub_gfx/cache.gosaveDetectCache函数中:

return os.WriteFile(detectCacheFile, data, 0644)

问题:0644权限意味着所有用户都可读,如果缓存文件包含敏感信息(如系统配置信息),可能会有信息泄露风险。

建议:考虑使用更严格的权限,如0600:

return os.WriteFile(detectCacheFile, data, 0600)

2. 性能问题

2.1 EDID数据排序效率

grub_common/common.go中:

sort.Slice(edids, func(i, j int) bool {
    return bytes.Compare(edids[i], edids[j]) < 0
})

问题:对于大量EDID数据,bytes.Compare可能不是最高效的比较方式。

建议:可以考虑使用更高效的比较方式,或者如果EDID数据量不大,可以保持现状。

2.2 缓存文件I/O操作

grub_gfx/cache.go中,每次检测都会读取和写入缓存文件:

data, err := os.ReadFile(detectCacheFile)
// ...
return os.WriteFile(detectCacheFile, data, 0644)

建议:考虑添加缓存文件的内存缓存,减少磁盘I/O操作。

3. 代码质量问题

3.1 错误处理

grub_gfx/main.go中:

edidsHash, err := grub_common.GetConnectedEdidsHash()
if err != nil {
    logger.Warning("failed to get connected edids hash:", err)
}

问题:获取EDID哈希失败后,代码仍然继续执行,可能导致缓存不一致。

建议:考虑在获取EDID哈希失败时,直接返回或使用默认值:

edidsHash, err := grub_common.GetConnectedEdidsHash()
if err != nil {
    logger.Warning("failed to get connected edids hash:", err)
    return // 或者使用默认值
}

3.2 日志记录

grub_gfx/main.go中,有大量的日志记录:

logger.Debugf("needDetect: cfgGfxmodeErr != nil: %v (cfgGfxmodeErr: %v)", condCfgGfxmodeErr, cfgGfxmodeErr)
logger.Debugf("needDetect: cfgGfxmode != currentGfxmode: %v (cfgGfxmode: %v, currentGfxmode: %v)", condCfgNeqCurrent, cfgGfxmode, currentGfxmode)
logger.Debugf("needDetect: currentGfxmode != maxGfxmode && detectCacheStale: %v (currentGfxmode: %v, maxGfxmode: %v, detectCacheStale: %v)", condCurrentNeqMaxAndCacheStale, currentGfxmode, maxGfxmode, detectCacheStale)
logger.Debug("needDetect result:", result)

建议:考虑将详细的调试日志封装到一个函数中,减少主流程中的日志代码:

func logNeedDetectDetails(condCfgGfxmodeErr, condCfgNeqCurrent, condCurrentNeqMaxAndCacheStale bool, cfgGfxmodeErr error, cfgGfxmode, currentGfxmode, maxGfxmode grub_common.Gfxmode) {
    logger.Debugf("needDetect: cfgGfxmodeErr != nil: %v (cfgGfxmodeErr: %v)", condCfgGfxmodeErr, cfgGfxmodeErr)
    logger.Debugf("needDetect: cfgGfxmode != currentGfxmode: %v (cfgGfxmode: %v, currentGfxmode: %v)", condCfgNeqCurrent, cfgGfxmode, currentGfxmode)
    logger.Debugf("needDetect: currentGfxmode != maxGfxmode && detectCacheStale: %v (currentGfxmode: %v, maxGfxmode: %v, detectCacheStale: %v)", condCurrentNeqMaxAndCacheStale, currentGfxmode, maxGfxmode, detectCacheStale)
}

3.3 注释和文档

grub_gfx/main.go中的needDetect函数有很好的注释,但其他函数缺少类似的文档注释。

建议:为所有公共函数添加文档注释,说明其用途、参数和返回值。

4. 逻辑问题

4.1 缓存验证逻辑

grub_gfx/main.go中:

detectCache, err := loadDetectCache()
logger.Debugf("loaded detect cache: %+v", detectCache)
if err == nil && detectCache.equal(edidsHash, maxGfxmode) {
    detectCacheStale = false
}

问题:即使缓存加载失败(err != nil),代码仍然会继续执行,可能导致不必要的检测。

建议:考虑更明确的缓存验证逻辑:

detectCache, err := loadDetectCache()
if err != nil {
    logger.Debugf("failed to load detect cache: %v", err)
    detectCacheStale = true
} else if detectCache.equal(edidsHash, maxGfxmode) {
    detectCacheStale = false
    logger.Debug("detect cache is valid")
} else {
    detectCacheStale = true
    logger.Debug("detect cache is stale")
}

总结

这次提交引入的缓存机制是一个很好的优化,可以减少不必要的图形模式检测。主要需要改进的是:

  1. 将MD5替换为更安全的哈希算法
  2. 加强缓存文件的权限控制
  3. 改进错误处理逻辑
  4. 优化日志记录方式
  5. 添加更多的文档注释

这些改进将提高代码的安全性、可维护性和健壮性。

@electricface
Copy link
Member Author

/forcemerge

@deepin-bot deepin-bot bot merged commit 1d29399 into linuxdeepin:master Mar 4, 2026
18 checks passed
@electricface electricface deleted the swt/grub-gfxmode-detect branch March 4, 2026 05:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants